wpf:通过绑定向ComboBoxItem添加样式
我有一个wpf:通过绑定向ComboBoxItem添加样式,wpf,binding,combobox,styles,Wpf,Binding,Combobox,Styles,我有一个组合框和一个样式用于项目。通过使用style.Triggers定义样式,方法如下: <Style> <Style.Triggers> <Trigger Property="Tag" Value="false"> <Setter Property="Background" Value="LightBlue"/> <Setter Property="Foregro
组合框
和一个样式
用于项目。通过使用style.Triggers
定义样式,方法如下:
<Style>
<Style.Triggers>
<Trigger Property="Tag" Value="false">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="Tag" Value="true">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
但是,如果我使用DataContext
绑定,如何嵌入这个样式
提前感谢。您可以尝试将样式设置器添加到组合框的资源中:
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Tag" Value="{Binding SomeValue}" />
</Style>
</ComboBox.Resources>
我相信触发器应该仍然有效,不管您是手动还是通过绑定设置标记的值。我迷路了。。。我有一些值列表,每个值实际上绑定到ComboBoxItem。正如我所说,绑定是由combo.DataContext=list完成的。。。标签属性放在哪里?Thanks@user301639标记值是否是值列表的一部分?如果是这样,这段代码将获取ComboBox中的每个ComboBoxItem,并为标记创建一个绑定。如果您只是尝试每隔一行交替使用样式,那么最好将组合框的AlternationCount
设置为2,并基于AlternationIndex
构建触发器。感谢您的想法,在msdn站点中找到了一些示例,解决了我的问题:)
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Tag" Value="{Binding SomeValue}" />
</Style>
</ComboBox.Resources>