Reputation: 123
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>
Upvotes: 2
Views: 6860
Reputation: 2202
The binding you set for your ComboBoxe
s Foreground
prop is looking for a ComboBoxItem
typed ancestor in the visual tree, but the item you need is the descendant of the ComboBox
. Especially the 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.
I recommend the following:
It seems to me that DoesNotParticipate is a boolean prop so don't use Converter
, use instead 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>
Upvotes: 1