Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

WPF ComboBox color item according to property Mvvm

Hi I have a simple question.

I am having a combobox that is based on Dictionary

ComboBox:

<ComboBox SelectedValuePath="Key" DisplayMemberPath="Value.ModuleName" 
    controls:TextBoxHelper.Watermark="All" Height="2"ItemsSource="{Binding Modules}"/>

Modules id a Dictionary:

public Dictionary<string, ModulesModel> Modules { get; set; }

ModulesModel and modules models is just:

public class ModulesModel
{
    public byte ModuleId { get; set; }
    public string ModuleName { get; set; }
    public bool IsWarning { get; set; }
}

So combo box fills fine but now i am trying to colour background of a combo box item if warning is set to true, so far i tried

<ComboBox.ItemTemplate>

 <DataTemplate>
     <DataTemplate.Triggers>
        //NOW HOW TO BING Value.IsWarning?
     </DataTemplate.Triggers>
 </DataTemplate>

 </ComboBox.ItemTemplate>

And no luck, is there a way?

Upvotes: 0

Views: 692

Answers (1)

user1672994
user1672994

Reputation: 10839

Use ItemContainerStyle instead of ItemTemplate

<ComboBox SelectedValuePath="Key" DisplayMemberPath="Value.ModuleName" 
    controls:TextBoxHelper.Watermark="All" Height="2"ItemsSource="{Binding Modules}">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsWarning }" Value="True">
                            <Setter Property="Backgroupd" Value="Blue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

Upvotes: 3

Related Questions