[英]How to find the child control's IsEnabled property in ListBoxItem for Trigger
I am trying to disable the ListBox item depending on IsEnabled property of ListBoxItem's content. 我试图根据ListBoxItem内容的IsEnabled属性禁用ListBox项目。 Like in this code the button 1 has IsEnabled=False but list box item is selectable.
像此代码中的按钮1具有IsEnabled = False,但列表框项目是可选的。 I want to disable the selection if contents IsEnabled property is false.
如果内容IsEnabled属性为false,我想禁用选择。 how should trigger search for the item content and its IsEnabled property.
如何触发搜索项目内容及其IsEnabled属性。
<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? 为什么不只在您的
ListBoxItem
上设置IsEnabled
? It should disable the Button
as well. 它也应该禁用
Button
。
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 但这就是说,您可以使用
RelativeSource
绑定将Button.IsEnabled
绑定到ListBoxItem.IsEnabled
,并设置ListBoxItem
的IsEnabled
属性,而不要设置Button
<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. 如果您使用WPF,强烈建议您研究MVVM设计模式。 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
它非常适合WPF,使用它您可以将
ListBoxItem.IsEnabled
和Button.IsEnabled
都绑定到DataContext
的同一属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.