[英]Using datatriggers to set the ItemsSource property in WPF
I have been trying to set the item source of a WPF control based on a enum that the data trigger is bound too. 我一直试图基于也绑定了数据触发器的枚举来设置WPF控件的项目源。
I have been very unsucessful and I am unsure of this is the correct way to do it: 我一直做不到成功,我不确定这是正确的方法:
<DataTrigger Binding="{Binding EnumSetting}" Value="Test">
<Setter TargetName="control" Property="ItemsSource" Value="{Binding Model}" />
</DataTrigger>
I have been trying different versions of the above. 我一直在尝试上述的不同版本。 Could anyone help or point me in the right direction.
任何人都可以帮助或指出我正确的方向。
Should it be wrapped in "<'style'>" tags for example, I don't think it should be but I am unsure why this is not working. 例如,如果应该将其包装在“ <'style'>”标签中,我不认为应该这样做,但是我不确定为什么它不起作用。
Thank you. 谢谢。
Mainly you need to watch DP precedence , which means you may not set the ItemsSource on the ItemsControl
(or sub-class of that) directly as it would override the trigger. 主要是您需要注意DP优先级 ,这意味着您不能直接在
ItemsControl
(或其子类)上设置ItemsSource,因为它将覆盖触发器。 Instead you should use a default setter. 相反,您应该使用默认的setter。
<ItemsControl>
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Setter Property="ItemsSource" Value="SomeDefaultHere"/>
<Style.Triggers>
<DataTrigger ...>
<Setter Property="ItemsSource" Value="SomeOverrideHere"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
(You probably want a style unless you are in some control template's triggers.) (除非您处于某些控制模板的触发器中,否则您可能想要一种样式。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.