wpf - 如何根据属性值禁用数据绑定(bind)的 ListBox 项?

标签 wpf xaml listbox datatemplate

有谁知道是否以及如何禁用数据绑定(bind)中的项目ListBox基于属性(property)的值(value)?

最好我想要一个 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>

关于wpf - 如何根据属性值禁用数据绑定(bind)的 ListBox 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1824543/

相关文章:

c# - 将 3D 点投影到 2D 屏幕坐标

c# - 如何获取 WPF 用户控件可见部分的大小?

wpf - 如何将样式绑定(bind)到具有样式元素的任何数据上下文的属性?

xaml - 如果内容按行方向放置,如何减少 Flex 布局中内容之间的空间?

c# - UWP 中有小时、分钟和秒的时间控制吗?

c# - MVVM 如何将 ListBox SelectedItem 放入 ViewModel 中的类的实例中

vb.net - 从对象内部的对象列表绑定(bind)列表框

listbox - WP7 - 单击列表框项目时调用绑定(bind)号码

vb6 - 禁用列表框VB6中的选定项目

wpf - 如何删除 ButtonChrome 边框(定义边框模板时)?