Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Change background on some combobox items

I'm trying to change the background of certain items in a combobox that meet a condition

<ComboBox ItemsSource="{Binding Path=Model.Names, Mode=OneWay}" SelectedValue="{Binding Path=SelectedCompanyName}" DisplayMemberPath="Alias" />

The thing is that "Alias" is saved in two different places (in company and in order) and if they dont match we want to highlight this.

I want to do something like this:

<Style>...
    <DataTrigger Binding="{Binding Path=isMismatch}" Value="True>
        <Setter Property="Background" Value="Red" />...

Any help is appreciated.

like image 277
debe Avatar asked Jan 01 '26 22:01

debe


1 Answers

You need to create custom data template like this:

<ComboBox Width="300" Height="30" ItemsSource="{Binding Path=Model.Names, Mode=OneWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="templateGrid">
                <TextBox Text="{Binding Name}" />
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding isMismatch}" Value="True">
                   <Setter TargetName="templateGrid" 
                           Property="Background" Value="Red" />         
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
like image 196
Nagg Avatar answered Jan 03 '26 12:01

Nagg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!