Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何找到子控件';触发器的ListBoxItem中的s IsEnabled属性_C#_Wpf_Xaml_Triggers_Isenabled - Fatal编程技术网

C# 如何找到子控件';触发器的ListBoxItem中的s IsEnabled属性

C# 如何找到子控件';触发器的ListBoxItem中的s IsEnabled属性,c#,wpf,xaml,triggers,isenabled,C#,Wpf,Xaml,Triggers,Isenabled,我试图根据ListBoxItem内容的IsEnabled属性禁用ListBoxItem。与此代码类似,按钮1的IsEnabled=False,但列表框项是可选的。如果内容IsEnabled属性为false,我想禁用选择。如何触发对项目内容及其IsEnabled属性的搜索 <Grid> <ListBox> <ListBox.ItemTemplate> <DataTemplate>

我试图根据ListBoxItem内容的IsEnabled属性禁用ListBoxItem。与此代码类似,按钮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>

1.
2.
3.

为什么不直接在您的
列表框项目上设置
IsEnabled
?它还应禁用
按钮

但也就是说,您可以使用
相对资源
绑定将
按钮.IsEnabled
绑定到
ListBoxItem.IsEnabled
,并设置
ListBoxItem
IsEnabled
属性,而不是
按钮

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


如果您正在使用WPF,我强烈建议您研究MVVM设计模式。它非常适合WPF,使用它可以将
ListBoxItem.IsEnabled
按钮绑定到
DataContext

中的同一属性,为什么不在
ListBoxItem
上设置
IsEnabled
?它还应禁用
按钮

但也就是说,您可以使用
相对资源
绑定将
按钮.IsEnabled
绑定到
ListBoxItem.IsEnabled
,并设置
ListBoxItem
IsEnabled
属性,而不是
按钮

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


如果您正在使用WPF,我强烈建议您研究MVVM设计模式。它非常适合WPF,使用它可以将
ListBoxItem.IsEnabled
按钮绑定到
DataContext

中的相同属性,谢谢,但这不是我想要实现的。我想获取子控件的IsEnabled属性。我正在使用MVVM,但我希望使用资源字典中的触发器样式。谢谢,但这不是我想要实现的。我想获取子控件的IsEnabled属性。我正在使用MVVM,但我希望使用资源字典中的样式触发器。