轻松设置所有ComboBox的默认颜色

时间:2019-06-30 14:46:10

标签: c# wpf templates combobox

我研究了几个主题,但是找不到一种简单的方法来定义组合框的默认颜色。

我现在正在尝试的是以下内容:

<Style TargetType="ComboBox">
   <Setter Property="BorderBrush" Value="{StaticResource WindowBorderColor}"/>
   <Setter Property="Background" Value="{StaticResource WindowBackgroundColor}"/>
   <Style.Triggers>
      <Trigger Property="IsEnabled" Value="False">
         <Setter Property="Background" Value="Yellow"/>
      </Trigger>
   </Style.Triggers>
</Style>
<Style TargetType="ComboBoxItem">
   <Setter Property="BorderBrush" Value="{StaticResource WindowBorderColor}"/>
   <Setter Property="Background" Value="{StaticResource WindowBackgroundColor}"/>
   <Setter Property="Template">
       <Setter.Value>
          <ControlTemplate TargetType="{x:Type ComboBoxItem}">
               <Border Background="{TemplateBinding Background}">
                   <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
               </Border>
              <ControlTemplate.Triggers>
                 <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{StaticResource WindowBackgroundColor}"/>
                 </Trigger>
                 <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource WindowBorderColor}"/>
                 </Trigger>
              </ControlTemplate.Triggers>
           </ControlTemplate>
        </Setter.Value>
     </Setter>
  </Style>

但是直到现在,它仍无法正常工作(背景仍然为灰色),我所能做的就是设置ComboBox项目的颜色:

enter image description here

编辑:

我可以设法做“某事”,正确地应用颜色,但是现在TexBox中的文本没有出现(在弹出窗口中正确出现):

<Style TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border x:Name="templateRoot" BorderBrush="Yellow"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Background="Green" 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="DarkCyan" HorizontalAlignment="Center"
                            Margin="0" VerticalAlignment="Center"/>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
    <Border x:Name="PART_ContentHost" Focusable="False" Background="White" />
</ControlTemplate>
<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="Blue"
            BorderThickness="1" />
        <Border 
            Grid.Column="0"
            CornerRadius="2,0,0,2" 
            Margin="1" 
            Background="green" 
            BorderThickness="0,0,1,0">

        </Border>

        <Path 
            x:Name="Arrow"
            Grid.Column="1"     
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Data="M 0 0 L 4 4 L 8 0 Z"/>
    </Grid>
</ControlTemplate>
<Style TargetType="ComboBox">
    <Setter Property="BorderBrush" Value="{StaticResource WindowBorderColor}"/>
    <Setter Property="Background" Value="{StaticResource WindowBackgroundColor}"/>
    <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="1,1,1,1"
                            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">

                            </Border>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>


                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:2)

您可以创建当前ComboBox样式的副本并进行编辑。 在设计窗口上的控件(ComboBox)上单击鼠标右键,然后转到Edit Template,然后选择Edit a Copy 这将创建默认模板,您可以根据需要对其进行编辑。

您可以编辑以下部分以设置默认背景色

<Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="{StaticResource WindowBackgroundColor}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{StaticResource ComboBox.Static.Border}">
   <Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
      <Path x:Name="arrow" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" 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"/>
   </Border>
</Border>