【问题标题】:ComboBox change selected item background color by trigger conditionComboBox 通过触发条件更改所选项目的背景颜色
【发布时间】:2018-04-20 16:10:04
【问题描述】:

我正在尝试对 wpf 中的组合框进行一些操作,首先我的组合框看起来像:

<ComboBox SelectedValuePath="Key" DisplayMemberPath="Value.ModuleName" controls:TextBoxHelper.Watermark="All" Height="2" IsSynchronizedWithCurrentItem="True" 
    ItemsSource="{Binding Modules}" commands:PropertyChangeBehavior.Command="{Binding ModuleCommand}" 
    SelectedValue="{Binding SelectedModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

<ComboBox.ItemContainerStyle>
     <Style TargetType="ComboBoxItem">
         <Style.Triggers>
              <DataTrigger Binding="{Binding Path=Value.IsWarning }" Value="True">
                    <Setter Property="Background" Value="#FF6666" />
              </DataTrigger>
          </Style.Triggers>
      </Style>
</ComboBox.ItemContainerStyle>

</ComboBox>

它的作用是,当组合框中的字典类属性警告设置为 true 时,我得到彩色背景,并且工作正常。 But it does not work when that element selected, is there any way to do when, selected element has is warning property set it would change it selected background as well and if its false it behaves normal, so far I tried adding one more trigger:

<DataTrigger Binding="{Binding Path=SelectedItem.Content}" Value="True">
   <Setter Property="Background" Value="#FF6666" />
</DataTrigger>

还有:

  <Trigger Property="IsSelected" Value="true">
    <Setter Property="Background" Value="#FF6666"/>
  </Trigger>

但运气不好,这有可能吗。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    当然有可能,是的。

    问题在于默认样式例如ComboBoxListBox 为其选定的项目设置触发器,这些项目不能被覆盖。因此我们需要为ComboBoxItem定义一个自定义模板。

    所以我想出了以下非常简单的ComboBox 样式作为您的起点。它远非完整或美观,但它提供了所需的功能。

    <Style TargetType="{x:Type ComboBoxItem}">
         <Style.Resources>
             <SolidColorBrush x:Key="WarningBrush" Color="#FF6666" />
             <SolidColorBrush x:Key="WarningHighlightedBrush" Color="#FF8888" />
             <SolidColorBrush x:Key="DefaultHighlightedBrush" Color="LightBlue" />
         </Style.Resources> 
         
         <Setter Property="Background" Value="{StaticResource WarningBrush}" />
             <Setter Property="Template">
                 <Setter.Value>
                     <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                         <Border x:Name="myBorder" Background="{TemplateBinding Background}">
                            <ContentPresenter />
                         </Border>
                         <ControlTemplate.Triggers>
                             <MultiDataTrigger>
                                 <MultiDataTrigger.Conditions>
                                     <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
                                     <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value.IsWarning}" Value="True"/>
                                 </MultiDataTrigger.Conditions>
                                 <Setter TargetName="myBorder" Property="Background" Value="{StaticResource WarningHighlightedBrush}" />
                             </MultiDataTrigger>
                             <MultiDataTrigger>
                                 <MultiDataTrigger.Conditions>
                                     <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
                                     <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value.IsWarning}" Value="False"/>
                                 </MultiDataTrigger.Conditions>
                                 <Setter TargetName="myBorder" Property="Background" Value="{StaticResource DefaultHighlightedBrush}" />
                             </MultiDataTrigger>
                         </ControlTemplate.Triggers>
                     </ControlTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
    

    上面的样式产生了这些结果:


    让我知道它是否也适合您,如果您对建议的样式有任何其他问题/疑问。

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多