如何基于属性值禁用数据绑定ListBox项?

J W*_*J W 30 wpf xaml listbox datatemplate

有没有人知道是否以及如何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>
Run Code Online (Sandbox Code Playgroud)

jap*_*apf 64

您可以使用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>
Run Code Online (Sandbox Code Playgroud)