【发布时间】:2016-10-14 17:24:39
【问题描述】:
我正在尝试构建一个允许用户取消选择项目的 ListBox。
这是我的 XAML 代码:
<ListBox Name="MyListBox"
ItemsSource="{Binding MoeglicheHauptspeisen}"
SelectionMode="Single">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="200"
Height="200"
Background="{StaticResource Braun}"
MouseDown="Speise_Gedrueckt">
<TextBlock Margin="0 50 0 0"
HorizontalAlignment="Center"
Text="{Binding SpeiseTyp}" />
<TextBlock HorizontalAlignment="Center" Text="{Binding Beschreibung}" />
<TextBlock HorizontalAlignment="Center" Text="{Binding Preis, StringFormat={}{0:C}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
还有我的代码隐藏:
private void Speise_Gedrueckt(object sender, MouseButtonEventArgs e)
{
MyListBox.UnselectAll();
}
我可以通过再次单击它来取消选择该项目,它确实将 SelectedIndex 设置为 -1 等,但它不会清除所选项目在 ListBox 本身中的边框。
我用谷歌搜索了很多,但没有发现任何东西改变了这一事实。我尝试设置通过 style.setter 选择的项目的边框/背景,但它也没有帮助。
【问题讨论】: