views:

729

answers:

1

Does anyone know if and how one can disable one or more listbox items in a wpf lisbox without disabling the entire listbox?

Preferably I would have a DataTrigger which, when a certain Binding value is false, disables this item (make it impossible to select) while keeping other items in the same listbox active.

<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>
+8  A: 

You can user ItemContainerStyle:

<ListBox>
  <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding YouProperty}" Value="False">
          <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>
Jalfp