.net WPF - 如何结合 DataTrigger 和 Trigger?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:14:44  来源:igfitidea点击:

WPF - How to combine DataTrigger and Trigger?

.netwpftriggersselectionlistboxitem

提问by Drew Noakes

NOTEI have asked the related question: How to combine DataTrigger and EventTrigger?

注意我已经问过相关的问题:How to combine DataTrigger and EventTrigger?

I have a list box containing several items. The item's class implements INotifyPropertyChangedand has a property IsAvailable. I use that property to indicate unavailable options in the list using a different colour.

我有一个包含多个项目的列表框。项目的类实现INotifyPropertyChanged并具有属性IsAvailable。我使用该属性以不同的颜色指示列表中不可用的选项。

However, if a selected item is not available, then the foreground colour should be red.

但是,如果所选项目不可用,则前景色应为红色。

<ListBox>
  <ListBox.Resources>
    <DataTemplate DataType="{x:Type local:InstitutionViewModel}">
      <TextBlock Name="Name" Text="{Binding Name}"/>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsAvailable}" Value="False">
          <Setter TargetName="Name" Property="Foreground" Value="#888"/>
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ListBox.Resources>
</ListBox>

I use the above data trigger to grey out unavailable items.

我使用上面的数据触发器将不可用的项目变灰。

The problem I'm facing is that the fact that the item is selected has nothing to do with the underlying data to which the template is bound. What I really want is some kind of multi-trigger that supports both a regular Triggeron a dependency property (ListBoxItem.IsSelected) along with a DataTriggeron the bound data item.

我面临的问题是,项目被选中的事实与模板绑定到的基础数据无关。我真正想要的是某种多触发器,它既支持Trigger依赖属性 ( ListBoxItem.IsSelected)上的正则,又支持DataTrigger绑定数据项上的一个。

Can this be done without introducing the concept of selection into my view model?

这可以在不将选择概念引入我的视图模型的情况下完成吗?

For anyone wondering why I do not disable unavailable items, understand that it is a requirement of the application that unavailable options may be selected. There are actually a few list boxes, and selection in one effects what's available in the others. I cannot disable the items as the user would not be able to change their minds or explore different combinations if items were disabled based upon earlier selections.

对于想知道为什么我不禁用不可用项目的任何人,请了解应用程序要求可以选择不可用选项。实际上有几个列表框,选择一个会影响其他的可用内容。我无法禁用这些项目,因为如果根据之前的选择禁用项目,用户将无法改变主意或探索不同的组合。

回答by Drew Noakes

For anyone else who's up against this problem, I found a solution that works for me. Of course, I'm still interested to see other interesting answers.

对于遇到此问题的其他任何人,我找到了一个适合我的解决方案。当然,我仍然有兴趣看到其他有趣的答案。

Here's what I did:

这是我所做的:

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition Binding="{Binding
      RelativeSource={
        RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
        Path=IsSelected}" Value="True"/>
    <Condition Binding="{Binding IsAvailable}" Value="False"/>
  </MultiDataTrigger.Conditions>
  <Setter TargetName="Name" Property="Foreground" Value="#F00"/>
</MultiDataTrigger>

There's nothing special about this being a multi trigger though. If you just wanted to style the selected item differently in your data template, you could use:

不过,这是一个多触发器并没有什么特别之处。如果您只想在数据模板中以不同的方式设置所选项目的样式,您可以使用:

<DataTrigger Binding="{Binding 
  RelativeSource={
    RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
    Path=IsSelected}" Value="True">
  <Setter TargetName="Name" Property="Foreground" Value="#888"/>
</DataTrigger>

回答by Vadim

To use it with DataGridRowchange binding mode to Self:

要将其与DataGridRow更改绑定模式一起使用Self

Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=...