开发者

How to unselect a listbox datatemplate item?

开发者 https://www.devze.com 2023-03-02 14:00 出处:网络
<TabItem Name=\"tbInActive\" Header=\"Previous\" Width=\"100\" Height=\"100\"> <ListBox Name=\"lbActive\"
<TabItem Name="tbInActive" Header="Previous" Width="100" Height="100">
    <ListBox Name="lbActive" 
             DockPanel.Dock="Top"
             ItemContainerStyle="{DynamicResource SelectedItemContainer}">
        <ListBox.ItemTemplate>
            <DataTemplate  >
                <EventDet:EventSumDetail x:Name="ItemCtrl"
                                         SelectedItem="{Binding ElementName=lbInActive, Path=SelectedItem}" />
            <开发者_如何学运维/DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <!-- ... -->
</TabItem>

In my codebehind I tried

this.lbActive.SelectedItem = null; and this.lbActive.UnselectAll(); and (edit) this.lbActive.SelectedIndex = -1;

But they had no effect.


There is no reason why this.lbActive.SelectedItem = null; should not work. (It works on a clean slate ListBox)

I'm quite sure the problem lies with your custom parts, either SelectedItem="{Binding ElementName=lbInActive, Path=SelectedItem}" is forcing a selection, or a binding in your ItemContainerStyle does so.


I guest that EventDet:EventSumDetail is a kind of ListBoxItem or something like that. The problem you had a OneWay binding(by default)... you had to explicitly make it TwoWay binding.. like this:

<EventDet:EventSumDetail SelectedItem="{Binding ElementName=lbInActive, Path=SelectedItem, Mode=TwoWay}" />

Also, you don't need to name (x:Name="ItemCtrl"), in this case it's unnecessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消