WPF: Change background on some combobox items

夙愿已清 提交于 2020-01-04 02:32:05

问题


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.


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/3397497/wpf-change-background-on-some-combobox-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!