![](/img/trans.png)
[英]How to bind the IsEnabled Property of a WPF ListBoxItem to a Property of its Source Item?
[英]How to find the child control's IsEnabled property in ListBoxItem for Trigger
我試圖根據ListBoxItem內容的IsEnabled屬性禁用ListBox項目。 像此代碼中的按鈕1具有IsEnabled = False,但列表框項目是可選的。 如果內容IsEnabled屬性為false,我想禁用選擇。 如何觸發搜索項目內容及其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>
為什么不只在您的ListBoxItem
上設置IsEnabled
? 它也應該禁用Button
。
但這就是說,您可以使用RelativeSource
綁定將Button.IsEnabled
綁定到ListBoxItem.IsEnabled
,並設置ListBoxItem
的IsEnabled
屬性,而不要設置Button
<Button IsEnabled="{Binding IsEnabled,
RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}" />
如果您使用WPF,強烈建議您研究MVVM設計模式。 它非常適合WPF,使用它您可以將ListBoxItem.IsEnabled
和Button.IsEnabled
都綁定到DataContext
的同一屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.