如何禁用基于属性值数据绑定列表框的项目?
-
22-07-2019 - |
题
有谁知道是否和如何可以根据属性值在数据绑定ListBox
禁用物品?
优选地,我想一个DataTrigger
其中,当某属性是false
,禁用此项目(使得不可能选择),而不在ListBox
影响其他物品。
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="textBlock" Text="{Binding Description}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
??
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
解决方案
可以使用ItemContainerStyle:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding YourPropertyName}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
不隶属于 StackOverflow