[英]WPF - Set SelectedItem property using the ItemsSource property
[英]Using datatriggers to set the ItemsSource property in WPF
我一直试图基于也绑定了数据触发器的枚举来设置WPF控件的项目源。
我一直做不到成功,我不确定这是正确的方法:
<DataTrigger Binding="{Binding EnumSetting}" Value="Test">
<Setter TargetName="control" Property="ItemsSource" Value="{Binding Model}" />
</DataTrigger>
我一直在尝试上述的不同版本。 任何人都可以帮助或指出我正确的方向。
例如,如果应该将其包装在“ <'style'>”标签中,我不认为应该这样做,但是我不确定为什么它不起作用。
谢谢。
主要是您需要注意DP优先级 ,这意味着您不能直接在ItemsControl
(或其子类)上设置ItemsSource,因为它将覆盖触发器。 相反,您应该使用默认的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>
(除非您处于某些控制模板的触发器中,否则您可能想要一种样式。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.