将 WPF ComboBox Selected Item Color 设置为 ComboBox Item 的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12396053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set WPF ComboBox Selected Item Color to the Color of the ComboBox Item
提问by New Developer
My combobox is bound to a list of states. The state have a column participating which when true the comboitem foreground color should be red. This works fine. But when I select the combobox item with foreground color red, it loses that foreground color and sets it to black. Can somebody help me point out what I am doing wrong?
我的组合框绑定到状态列表。状态有一个参与的列,当为真时,组合项的前景色应该是红色。这工作正常。但是当我选择前景色为红色的组合框项目时,它会丢失前景色并将其设置为黑色。有人可以帮我指出我做错了什么吗?
<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
DisplayMemberPath="StateName"
ItemsSource="{Binding States}"
SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="{Binding DoesNotParticipate, Converter={StaticResource NonParticipatingConverter}}"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
回答by Miklós Balogh
The binding you set for your ComboBoxes Foregroundprop is looking for a ComboBotItemtyped ancestor in the visual tree, but the item you need is the descendant of the ComboBox. Especially the ComboBoxes SelectedItem.
您为ComboBoxesForeground道具设置的绑定正在ComboBotItem可视化树中寻找类型化祖先,但您需要的项目是ComboBox. 尤其是ComboBoxes SelectedItem.
EDIT
编辑
Since you bind the item to model objects the ComboBox.SelectedItems type would be this model objects type. Which means - probably - they don't have Foreground property.
由于您将项目绑定到模型对象,因此 ComboBox.SelectedItems 类型将是此模型对象类型。这意味着 - 可能 - 他们没有 Foreground 属性。
I recommend you the following:
It seems to me that DoesNotParticipate is a boolean prop so don't use Converter, use instead Trigger:
我向您推荐以下内容:在我看来,DoesNotParticipate 是一个布尔属性,所以不要使用Converter,而是使用Trigger:
<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
DisplayMemberPath="StateName"
ItemsSource="{Binding States}"
SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.DoesNotParticipate}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<DataTrigger Property={Binding DoesNotParticipate} Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>

