How to disable a databound ListBox item based on a property value?
Asked Answered
P

1

33

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

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>
Picayune answered 1/12, 2009 at 7:26 Comment(0)
P
70

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>
Philine answered 1/12, 2009 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.