簡體   English   中英

如何在觸發器的ListBoxItem中找到子控件的IsEnabled屬性

[英]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 ,並設置ListBoxItemIsEnabled屬性,而不要設置Button

<Button IsEnabled="{Binding IsEnabled, 
    RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}" />

如果您使用WPF,強烈建議您研究MVVM設計模式。 它非常適合WPF,使用它您可以將ListBoxItem.IsEnabledButton.IsEnabled都綁定到DataContext的同一屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM