【问题标题】:WPF Change Background color of a ComboboxWPF更改组合框的背景颜色
【发布时间】:2019-05-15 11:01:41
【问题描述】:

在我的 WPF 应用程序中,我只想更改组合框的背景颜色。我不是指下拉菜单,我想要的只是选择了任何项目并设置了背景。 就像设置按钮的背景一样 - 当控件显示在屏幕上时,它应该具有浅黄色背景。而已。 我在网上搜索了很多,但到处都可以找到下拉背景颜色的解决方案。我尝试将 SolidColorBrush 和 Style.Triggers 应用于 Combobox 的 TextBlock,但没有成功。通过添加 SolidColorBrush 线条,我设置了下拉背景,但这不是我想要的。我的代码是:

<ComboBox ItemsSource="{Binding MtrCm}" SelectedValue="{Binding WellboreDiameter_Unit, Mode=TwoWay}" Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,26,249,0" x:Name="cboWellDiameter" VerticalAlignment="Top" Width="120"   Background="LightYellow"  >
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Resources>
</ComboBox>

谁能帮我设置他正在寻找的所需组件的背景。

谢谢

【问题讨论】:

    标签: c# wpf combobox background-color


    【解决方案1】:

    试试这个

     <Window.Resources>  //Put this resourse n Window.Resources or UserControl.Resources
       <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
          <GradientBrush.GradientStops>
             <GradientStopCollection>
                <GradientStop Color="#FFDC3939" Offset="0.0"/>
                <GradientStop Color="#FFE80E0E" Offset="1.0"/>
             </GradientStopCollection>
          </GradientBrush.GradientStops>
       </LinearGradientBrush>
    
        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFFBE618" />
    
        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2"
      Background="{StaticResource NormalBrush}"
      BorderThickness="1" />
                <Border 
      Grid.Column="0"
      CornerRadius="2,0,0,2" 
      Margin="1" 
      Background="{StaticResource WindowBackgroundBrush}" 
      BorderThickness="0,0,1,0" />
                <Path 
      x:Name="Arrow"
      Grid.Column="1"     
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Data="M 0 0 L 4 4 L 8 0 Z"/>
            </Grid>
        </ControlTemplate>
    
        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
            <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
        </ControlTemplate>
    
    
    <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
      <Setter Property="Template">
       <Setter.Value>
         <ControlTemplate TargetType="ComboBox">
          <Grid>
           <ToggleButton 
             Name="ToggleButton" 
             Template="{StaticResource ComboBoxToggleButton}" 
             Grid.Column="2" 
             Focusable="false"
             IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
             ClickMode="Press">
          </ToggleButton>
          <ContentPresenter
            Name="ContentSite"
            IsHitTestVisible="False" 
            Margin="3,3,23,3"
            VerticalAlignment="Center"
            HorizontalAlignment="Left" />
           <TextBox x:Name="PART_EditableTextBox"
             Style="{x:Null}" 
             Template="{StaticResource ComboBoxTextBox}" 
             HorizontalAlignment="Left" 
             VerticalAlignment="Center" 
             Margin="3,3,23,3"
             Focusable="True" 
             Background="Transparent"
             Visibility="Hidden"
             IsReadOnly="{TemplateBinding IsReadOnly}"/>
          <Popup 
            Name="Popup"
            Placement="Bottom"
            IsOpen="{TemplateBinding IsDropDownOpen}"
            AllowsTransparency="True" 
            Focusable="False"
            PopupAnimation="Slide">
            <Grid 
              Name="DropDown"
              SnapsToDevicePixels="True"                
              MinWidth="{TemplateBinding ActualWidth}"
              MaxHeight="{TemplateBinding MaxDropDownHeight}">
               <Border 
                x:Name="DropDownBorder"
                Background="{StaticResource WindowBackgroundBrush}"
                BorderThickness="1"/>
               <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
               </ScrollViewer>
              </Grid>
             </Popup>
            </Grid>
           </ControlTemplate>
          </Setter.Value>
         </Setter>
        <Style.Triggers>
       </Style.Triggers>
      </Style>
     </Window.Resources>
     <Grid>
        <ComboBox HorizontalAlignment="Left" Margin="256,57,0,0" VerticalAlignment="Top" Width="120">
        </ComboBox>
    
     </Grid>
    

    这是您可以更改的完整样式:http://msdn.microsoft.com/en-us/library/ms752094%28v=VS.85%29.aspx

    【讨论】:

    • 所选项目未出现在顶部。尝试了许多变化来设置前景色,但没有影响。为什么所选项目在顶部不可见?我的意思是即使在选择后组合框也显示为空白 - 而所选项目应该出现在顶部。相同的设置是什么?
    • @Tvd 由于 ContentPresenter 缺少某些属性,所选项目未出现:Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
    • WPF 太令人失望了……这么多代码只是为了设置 ComboBox 的背景。
    • 谢谢@Optimax 答案应该用你的 cmets 更新!
    • 这是一个很好的例子,说明了为什么 WPF 如此混乱(甚至是失败?)。他们应该从头开始。
    【解决方案2】:

    如果您在查看 xaml 时在“文档大纲”窗口中使用 VS 2012 或 VS 2013,请获取组合框的控件模板。 找到样式 x:Key="ComboBoxReadonlyToggleButton" 并更新 Themes:ButtonChrome 上的 RenderMouseOver = "False" 和 RenderPressed="False" 并注释掉影响 IsEnabled 的触发器。

    下面是我在我的盒子上编辑的那个来说明我的意思。

    <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="ClickMode" Value="Press"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="False" RenderPressed="False" SnapsToDevicePixels="true">
                            <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
                            </Grid>
                        </Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="False"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <!--<Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>-->
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

    • 为此我需要添加 Aero 或任何此类组件,否则这也满足我的需要。非常感谢。
    【解决方案3】:

    虽然我喜欢接受的答案,但我遇到的问题是文本框演示者完全错过了,因此您可以选择和设置样式,但它们永远不会呈现给最终用户。我认为实现显示数据的组合框的最简单方法是:

    <Window http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Test" x:Class="MainWindow"
            mc:Ignorable="d" Title="MainWindow" Height="100" Width="200">
      <Window.Resources>
        <Style x:Key="ComboBoxTest2" TargetType="{x:Type ComboBox}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ComboBox">
                <Grid>
                  <ToggleButton Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" >
                    <ToggleButton.Template>
                      <ControlTemplate>
                        <Grid>
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="5*" />
                            <ColumnDefinition Width="*" />
                          </Grid.ColumnDefinitions>
                          <Border x:Name="Border"  Grid.ColumnSpan="2" CornerRadius="5" Background="Beige" BorderBrush="Black" BorderThickness="1" />
                          <Border Grid.Column="0" CornerRadius="5,0,0,5"  Margin="1"  Background="AliceBlue"  BorderBrush="Black" BorderThickness="0,0,1,0" />
                          <Path x:Name="Arrow" Grid.Column="1"  Fill="Orange" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                          <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Green" />
                          </Trigger>
                          <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Green" />
                          </Trigger>
                        </ControlTemplate.Triggers>
                      </ControlTemplate>
                    </ToggleButton.Template>
                  </ToggleButton>
                  <ContentPresenter Name="ContentSite" IsHitTestVisible="False"  Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3"  />
                  <TextBox x:Name="PART_EditableTextBox" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"/>
                  <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"  Focusable="False" PopupAnimation="Slide">
                    <Grid  Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                      <Border x:Name="DropDownBorder" Background="Blue" />
                      <ScrollViewer SnapsToDevicePixels="True">
                        <StackPanel IsItemsHost="True" />
                      </ScrollViewer>
                    </Grid>
                  </Popup>
                </Grid>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
          <Style.Triggers>
          </Style.Triggers>
        </Style>
      </Window.Resources>
      <StackPanel Margin="10">
        <ComboBox VerticalAlignment="Top" Width="120" Style="{StaticResource ComboBoxTest2}">
          <ComboBoxItem>Item1</ComboBoxItem>
          <ComboBoxItem>Item2</ComboBoxItem>
        </ComboBox>
      </StackPanel>
    </Window>
    

    演示中需要完成的关键部分是用于初始显示的模板“ToggleButton.Template”。我选择忽略制作更多画笔和指向更多模板的链接,而只是将其全部内联,以便其他人可以根据需要使用它。我还选择了我认为很容易识别的颜色以供参考,当它起作用时应该如下所示:

    【讨论】:

      【解决方案4】:

      只需将以下内容添加到您的 ComboBox 样式中即可:)

      BasedOn="{StaticResource {x:Type ComboBox}}"
      

      例子:

      <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
          <Setter Property="Background" Value="Red" />
          //Other setters, triggers, etc...
      </Style>
      

      相信我,这可能是最短的方法......我不明白为什么我们必须完全重新定义 ComboBox 模板。

      【讨论】:

      • 它什么也没做
      • 我收到此错误消息:无法解析资源“{x:Type ComboBox}”。
      【解决方案5】:

      在 Windows 8 或 10 上,这是正确的 WPF 方式,无需重新设计组合框并且 100% 可自定义。

      只需复制粘贴并测试! :)

      Click here to see how look the ComboBox

        <Window
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:Test_Combo_Custom"
          xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Class="Test_Combo_Custom.MainWindow"
          mc:Ignorable="d"
          Title="MainWindow" Height="450" Width="800">
      
      
      <Window.Resources>
          <Style x:Key="FocusVisual">
              <Setter Property="Control.Template">
                  <Setter.Value>
                      <ControlTemplate>
                          <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
          <SolidColorBrush x:Key="ComboBox.Static.Background" Color="Yellow"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Background" Color="#FFFF0C00"/>
          <SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/>
          <SolidColorBrush x:Key="ComboBox.Static.Editable.Background" Color="#FFFB0F0F"/>
          <SolidColorBrush x:Key="ComboBox.Static.Editable.Border" Color="#FFABADB3"/>
          <SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Background" Color="Transparent"/>
          <SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Border" Color="Transparent"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Background" Color="#FF88E00A"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Border" Color="#FF7EB4EA"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Background" Color="#FF7EB4EA"/>
          <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Border" Color="#FF7EB4EA"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Glyph" Color="#FF000000"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Background" Color="#FF000000"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Border" Color="#FF569DE5"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Background" Color="#FF00FFC5"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Border" Color="#FF569DE5"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Button.Background" Color="#FF569DE5"/>
          <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Button.Border" Color="#FF569DE5"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FF101AEC"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Background" Color="#FFC91355"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Border" Color="#FFBFBFBF"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Background" Color="Transparent"/>
          <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Border" Color="Transparent"/>
          <SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/>
      
      
          <Style x:Key="ComboBoxToggleButton2" TargetType="{x:Type ToggleButton}">
              <Setter Property="OverridesDefaultStyle" Value="true"/>
              <Setter Property="IsTabStop" Value="false"/>
              <Setter Property="Focusable" Value="false"/>
              <Setter Property="ClickMode" Value="Press"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type ToggleButton}">
                          <Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource ComboBox.Static.Background}" SnapsToDevicePixels="true">
                              <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                  <Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                              </Border>
                          </Border>
                          <ControlTemplate.Triggers>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
                                      <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="false"/>
                                      <Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="false"/>
                                      <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="true"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Border}"/>
                                  <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Border}"/>
                              </MultiDataTrigger>
                              <Trigger Property="IsMouseOver" Value="true">
                                  <Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.MouseOver.Glyph}"/>
                              </Trigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/>
                              </MultiDataTrigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Border}"/>
                                  <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Border}"/>
                              </MultiDataTrigger>
                              <Trigger Property="IsPressed" Value="true">
                                  <Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Pressed.Glyph}"/>
                              </Trigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="true"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Border}"/>
                              </MultiDataTrigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="true"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Border}"/>
                                  <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Border}"/>
                              </MultiDataTrigger>
                              <Trigger Property="IsEnabled" Value="false">
                                  <Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Disabled.Glyph}"/>
                              </Trigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Border}"/>
                              </MultiDataTrigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
                                      <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
                                  </MultiDataTrigger.Conditions>
                                  <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Border}"/>
                                  <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Background}"/>
                                  <Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Border}"/>
                              </MultiDataTrigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
          <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
              <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*"/>
                      <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                  </Grid.ColumnDefinitions>
                  <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                      <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                          <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                              <ScrollViewer x:Name="DropDownScrollViewer">
                                  <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                      <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                          <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                                      </Canvas>
                                      <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                  </Grid>
                              </ScrollViewer>
                          </Border>
                      </Themes:SystemDropShadowChrome>
                  </Popup>
                  <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton2}"/>
                  <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
              </Grid>
              <ControlTemplate.Triggers>
                  <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                      <Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/>
                      <Setter Property="Color" TargetName="shadow" Value="#71000000"/>
                  </Trigger>
                  <Trigger Property="HasItems" Value="false">
                      <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
                  </Trigger>
                  <MultiTrigger>
                      <MultiTrigger.Conditions>
                          <Condition Property="IsGrouping" Value="true"/>
                          <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                      </MultiTrigger.Conditions>
                      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                  </MultiTrigger>
                  <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                      <Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                      <Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                  </Trigger>
              </ControlTemplate.Triggers>
          </ControlTemplate>
          <SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF"/>
          <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
              <Setter Property="OverridesDefaultStyle" Value="true"/>
              <Setter Property="AllowDrop" Value="true"/>
              <Setter Property="MinWidth" Value="0"/>
              <Setter Property="MinHeight" Value="0"/>
              <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
              <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
              <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type TextBox}">
                          <ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
          <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
              <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*"/>
                      <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                  </Grid.ColumnDefinitions>
                  <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                      <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                          <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                              <ScrollViewer x:Name="DropDownScrollViewer">
                                  <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                      <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                          <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                                      </Canvas>
                                      <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                  </Grid>
                              </ScrollViewer>
                          </Border>
                      </Themes:SystemDropShadowChrome>
                  </Popup>
                  <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton2}"/>
                  <Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}">
                      <TextBox x:Name="PART_EditableTextBox" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                  </Border>
              </Grid>
              <ControlTemplate.Triggers>
                  <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                  </Trigger>
                  <Trigger Property="IsKeyboardFocusWithin" Value="true">
                      <Setter Property="Foreground" Value="Black"/>
                  </Trigger>
                  <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                      <Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/>
                      <Setter Property="Color" TargetName="shadow" Value="#71000000"/>
                  </Trigger>
                  <Trigger Property="HasItems" Value="false">
                      <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
                  </Trigger>
                  <MultiTrigger>
                      <MultiTrigger.Conditions>
                          <Condition Property="IsGrouping" Value="true"/>
                          <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                      </MultiTrigger.Conditions>
                      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                  </MultiTrigger>
                  <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                      <Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                      <Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                  </Trigger>
              </ControlTemplate.Triggers>
          </ControlTemplate>
          <Style x:Key="ComboBoxStyleCustom" TargetType="{x:Type ComboBox}">
              <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
              <Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/>
              <Setter Property="BorderBrush" Value="{StaticResource ComboBox.Static.Border}"/>
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
              <Setter Property="BorderThickness" Value="1"/>
              <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
              <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
              <Setter Property="Padding" Value="6,3,5,3"/>
              <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
              <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
              <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
              <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
              <Style.Triggers>
                  <Trigger Property="IsEditable" Value="true">
                      <Setter Property="IsTabStop" Value="false"/>
                      <Setter Property="Padding" Value="2"/>
                      <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
                  </Trigger>
              </Style.Triggers>
          </Style>
      </Window.Resources>
      
      
      <Grid>
          <ComboBox HorizontalAlignment="Left" Margin="317.429,194.428,0,0" VerticalAlignment="Top" Width="120" Style="{DynamicResource ComboBoxStyleCustom}">
              <ComboBox.Resources>
                  <!-- color of ComboBoxItem -->
                  <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF17FF00" />
              </ComboBox.Resources>
              <ComboBoxItem>One</ComboBoxItem>
              <ComboBoxItem>Two</ComboBoxItem>
              <ComboBoxItem>Three</ComboBoxItem>
          </ComboBox> 
      
      </Grid>
      </Window>
      

      【讨论】:

      • 这是什么“不重新设计ComboBox”
      【解决方案6】:

      可以将 TextBlock 放在组合框上,但 TextBlock 应该比 ComboBox 小,这样下拉按钮才不会被隐藏。在 TextBlock 中,可以轻松设置所需的背景颜色。当 ComboBox 中的选定项发生变化时,文本块必须更新。

      XAML 代码:

      <ComboBox x:Name="TestComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="145" 
                Margin="35, 5, 0, 0" SelectionChanged="TestComboBox_SelectionChanged"/>
      <TextBlock x:Name="TestTextBlock" Text="" HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Margin="36, 6, 0, 0" Padding="4,1" Width="126" Height="20">
        <TextBlock.Background>
          <SolidColorBrush Color="Yellow"/>
        </TextBlock.Background>
      </TextBlock>
      

      C#代码:

      private void TestComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
        ComboBoxItem cbi = (ComboBoxItem)TestComboBox.SelectedItem;
        if (cbi != null)
        {
          TestTextBlock.Text = cbi.Content.ToString();
        }
        else TestTextBlock.Text = "";
      }
      

      【讨论】:

        猜你喜欢
        • 2016-07-09
        • 2022-01-21
        • 2018-12-27
        • 2011-07-26
        • 2013-01-15
        • 2016-07-31
        • 1970-01-01
        • 2011-11-24
        相关资源
        最近更新 更多