How to unselect a listbox datatemplate item?
<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.
精彩评论