如何基于属性值禁用数据绑定的ListBox项? [英] How to disable a databound ListBox item based on a property value?

查看:65
本文介绍了如何基于属性值禁用数据绑定的ListBox项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否可以基于属性值禁用数据绑定ListBox中的项目吗?

Does anyone know if and how one can disable items in a databound ListBox based on the value of a property?

最好我要一个DataTrigger,当某个属性为false时,它会禁用该项目(使其无法选择),而不会影响ListBox中的其他项目.

Preferably I would like a DataTrigger which, when a certain property is false, disables this item (make it impossible to select) without affecting other items in the 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:

You can use 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>

这篇关于如何基于属性值禁用数据绑定的ListBox项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆