WPF: changing the background for some list items

I am trying to change the background of some elements in combobox that satisfy the condition

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

The fact is that Alias ​​is stored in two different places (in the company and in order), and if they do not 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.

+3
source share
2 answers

You need to create a custom data template as follows:

<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>
+4
source

If you want to highlight a selection based on the values ​​of two properties, I think you could use a MultiValueConverter along with a MultiBinding .

0

Source: https://habr.com/ru/post/1757835/


All Articles