简体   繁体   English

如何在触发器的ListBoxItem中找到子控件的IsEnabled属性

[英]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 ,并设置ListBoxItemIsEnabled属性,而不要设置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.IsEnabledButton.IsEnabled都绑定到DataContext的同一属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 WPF ListBoxItem 的 IsEnabled 属性绑定到其源项的属性? - How to bind the IsEnabled Property of a WPF ListBoxItem to a Property of its Source Item? 将 ListBoxItem IsSelected 触发器传播到子控件 - Propagate ListBoxItem IsSelected trigger to child control 有没有办法通过其子项的可见性属性隐藏 ListBoxItem - Is there a way to hidden ListBoxItem by Visibility property of it's child 当该按钮的IsEnabled绑定到某些视图模型时,Groupbox控件的IsEnabled状态不会传播到该子按钮 - Groupbox control IsEnabled state not propagated to child button when that button's IsEnabled is bound to some view model 如何从选定的ListBox控件的listboxitem获取属性? C# - How do i get a property from a selected ListBox Control 's listboxitem ? C# 在WPF中的ListBoxItem上触发触发器时,如何设置父元素的属性 - How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF 如何更新ListBoxItem属性 - How to update ListBoxItem property 将控件的IsEnabled属性与另一个布尔属性同步 - Synchronize the IsEnabled property of a control with another boolean property 如何在使用回发触发器时查找子页面控件 - How to find child page control while using postback trigger 如何在IsMouseOver触发器上更改UserControl的子元素的属性? - How to change a property of a UserControl's child element on a IsMouseOver trigger?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM