开发者

WPF: Change background on some combobox items

开发者 https://www.devze.com 2023-01-09 20:48 出处:网络
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=SelectedCo

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 apprecia开发者_Go百科ted.


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消