Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf:通过绑定向ComboBoxItem添加样式_Wpf_Binding_Combobox_Styles - Fatal编程技术网

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>