为什么我无法将 DataTrigger 添加到控件的触发器集合中?
为什么我不能这样编码
<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>
我收到此错误
Failed object initialization (ISupportInitialize.EndInit).
Triggers collection members must be of type EventTrigger.
Error at object '4_T' in markup file
我做错了什么请帮忙。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种无限制触发器的方法。
示例:
链接示例
链接组件Github
Here is a way for no limitations triggers.
Example:
Link Sample
Link Component Github
不幸的是,只有
EventTriggers
可以直接应用于元素。如果您想使用Trigger
或DataTrigger
,它们必须采用Style
、ControlTemplate
或<代码>数据模板。从资源名称来看,它看起来像是
ListBoxItem
ControlTemplate
内的Border
。您可以轻松地将触发器移至模板的触发器集合中。Unfortunately, only
EventTriggers
can be applied directly to elements. If you want to use aTrigger
orDataTrigger
, they have to be in aStyle
,ControlTemplate
, orDataTemplate
.From the resource names, it looks like this is a
Border
inside aListBoxItem
ControlTemplate
. You could easily move the triggers into the template's triggers collection.安倍是正确的,并很好地解释了局限性。您可能需要考虑的一件事是:
不要使用两种边框样式,并尝试根据触发器在它们之间进行选择...
在边框上使用单一样式,此样式的设置器代表您的“正常”外观。
此样式还包含您的 DataTrigger,并且您的 DataTrigger 有一组 setter,它们本质上代表您的第二种样式(当此触发器评估为 true 时,其优先级高于标准 setter!
编辑:
类似这样的 -
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!
Edit:
Something like this -