2012-03-21 47 views
2

我想根据ListBoxItem的内容的IsEnabled属性来禁用ListBox项目。就像在这段代码中,按钮1有IsEnabled = False,但是列表框项是可选的。如果内容IsEnabled属性为false,我想禁用选择。如何触发搜索项目内容及其IsEnabled属性。如何在触发器ListBoxItem中找到子控件的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> 

回答

0

为什么不设置你的ListBoxItem代替IsEnabled?它也应该禁用Button

话虽如此,你可以绑定Button.IsEnabledListBoxItem.IsEnabled使用RelativeSource绑定,并设置ListBoxItemIsEnabled财产,而不是Button

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

如果你使用WPF的工作,我d强烈建议您查看MVVM设计模式。它非常适合WPF,并且使用它你可以将ListBoxItem.IsEnabledButton.IsEnabled绑定到DataContext

+1

中的同一个属性上,但这不是我想要实现的。我想获得子控件的IsEnabled属性。 我正在使用MVVM,但我想在我的资源字典中使用触发器。 – DronBoard 2012-03-21 13:58:19