The following scenario:
<ComboBox ItemsSource="{Binding Path=Names}"
SelectedItem="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
Names are a set of strings that can be changed using code in the ViewModel. For example, names can be set as masculine names (John, Paul, George), and feminine names (Mary, Jane, Karen).
Suppose the collection of men is active and the user selects "John", making it SelectedItem. Now the collection is changing to female names. By default, SelectedItem will be set to Null, and John will no longer be displayed. Instead, an empty string will be displayed.
What I would like to achieve is that "John" still displays as SelectedItem, although it is no longer in the collection. However, since it is not part of the collection, I would like to change the color of SelectedItem to red.
I played with the IsEditable property, but I did not like changing the look of the ComboBox. The user should not in any case enter the text manually.
I tried to use different templates and styles (e.g. Template, ItemTemplate, ItemContainerStyle), but did not find a way to use it to my advantage.
Is it possible to customize the provided ComboBox the way I need it, or do I need to create my own custom control?
EDIT: I found a solution that I can live with.
<ComboBox IsEditable="True" IsReadOnly="True"
ItemsSource="{Binding Path=Names}"
Text="{Binding Path=Name}">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsNameInCollection}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Black"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
ViewModel , Name . IsNameInCollection Name .