wpf 为什么不能将 DataTrigger 添加到控件的 Triggers 集合中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3576193/
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
Why can't I add a DataTrigger to my control's Triggers collection?
提问by Tan
Why cant I code like this
为什么我不能这样编码
<Border Width="130" Height="70">
<Border.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
<Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorder}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
<Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorderInactive}"/>
</DataTrigger>
</Border.Triggers>
</Border>
I get this error
我收到这个错误
Failed object initialization (ISupportInitialize.EndInit).
Triggers collection members must be of type EventTrigger.
Error at object '4_T' in markup file
What am I doing wrong plz help.
我在做什么错请帮忙。
回答by Scott
Abe is correct and explains the limitations well. One thing you might want to consider is:
安倍是对的,并且很好地解释了局限性。您可能需要考虑的一件事是:
Instead of having two border styles, and trying to pick between them based on a trigger...
而不是有两种边框样式,并试图根据触发器在它们之间进行选择......
Use a single style on your border, this style's setters represent your 'normal' look. This style also contains your DataTrigger, and your DataTrigger has a collection of setters which essentially represents your second style (which have higher priority than the standard setters when this trigger evaluates to true!
在您的边框上使用单一样式,此样式的设置器代表您的“正常”外观。此样式还包含您的 DataTrigger,并且您的 DataTrigger 具有一组 setter,它们基本上代表您的第二个样式(当此触发器评估为 true 时,它们的优先级高于标准 setter!
Edit:
编辑:
Something like this -
像这样的东西——
<Style TargetType="Border" x:Key="BorderStyle">
<!-- These setters are the same as your normal style when none of your triggers are true -->
<Setter Property="BorderBrush" Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
<!-- These setters are the same as your ListBoxItemBorder style -->
<Setter Property="BorderBrush" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
<!-- These setters are the same as your ListBoxItemBorderInactive style -->
<Setter Property="BorderBrush" Value="Gray" />
</DataTrigger>
</Style.Triggers>
</Style>
回答by Abe Heidebrecht
Unfortunately, only EventTriggers
can be applied directly to elements. If you want to use a Trigger
or DataTrigger
, they have to be in a Style
, ControlTemplate
, or DataTemplate
.
不幸的是,只能EventTriggers
直接应用于元素。如果你想使用一个Trigger
或者DataTrigger
,他们是在一个Style
,ControlTemplate
或DataTemplate
。
From the resource names, it looks like this is a Border
inside a ListBoxItem
ControlTemplate
. You could easily move the triggers into the template's triggers collection.
从资源名称来看,这似乎是一个Border
内部的ListBoxItem
ControlTemplate
. 您可以轻松地将触发器移动到模板的触发器集合中。
回答by Ali Yousefi
Here is a way for no limitations triggers.
这是一种无限制触发器的方法。
Example:
例子:
<Border Width="130" Height="100" Grid.Row="1">
<ListBox x:Name="lstItems" ItemsSource="{Binding TestItems}">
</ListBox>
<tg:TriggerExtensions.Triggers>
<tg:TriggerCollections>
<tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0">
<tg:DataTriggerInfo.Setters>
<tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxRed}"/>
</tg:DataTriggerInfo.Setters>
</tg:DataTriggerInfo>
<tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0" IsInvert="True">
<tg:DataTriggerInfo.Setters>
<tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxBlue}"/>
</tg:DataTriggerInfo.Setters>
</tg:DataTriggerInfo>
</tg:TriggerCollections>
</tg:TriggerExtensions.Triggers>
</Border>
Link Sample
链接示例
Link Component Github
链接组件Github