I am trying to disable the ListBox item depending on IsEnabled property of ListBoxItem's content. Like in this code the button 1 has IsEnabled=False but list box item is selectable. I want to disable the selection if contents IsEnabled property is false. how should trigger search for the item content and its IsEnabled property.
<Grid>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBoxItem>
<Button IsEnabled="False">1</Button>
</ListBoxItem>
<ListBoxItem>
<Button>2</Button>
</ListBoxItem>
<ListBoxItem>
<Button>3</Button>
</ListBoxItem>
</ListBox>
</Grid>
Why not just set IsEnabled
on your ListBoxItem
instead? It should disable the Button
as well.
But that said, you could bind the Button.IsEnabled
to ListBoxItem.IsEnabled
using a RelativeSource
binding, and set the IsEnabled
property of the ListBoxItem
, not the Button
too
<Button IsEnabled="{Binding IsEnabled,
RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}" />
If you're working with WPF, I'd highly recommend you look into the MVVM design pattern. It's well suited for WPF, and using it you would bind both the ListBoxItem.IsEnabled
and Button.IsEnabled
to the same property in the DataContext
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.