无法更改XAML组合框的背景 [英] Can't Change the background of xaml combobox

查看:63
本文介绍了无法更改XAML组合框的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF项目,需要在XAML或c#中更改Combobox的背景颜色,我已经尝试使用一些此代码,但是它只是更改了下拉菜单的背景颜色,如图所示。

 < ComboBox Background = Yellow> 
< ComboBox.Resources>
< SolidColorBrush x:Key = {x:Static SystemColors.WindowBrushKey} Color = Yellow />
< SolidColorBrush x:Key = {x:Static SystemColors.HighlightBrushKey} Color = Red />
< /ComboBox.Resources>
< ComboBox.ItemContainerStyle>
< Style TargetType = ComboBoxItem>
< Setter Property = Background Value = Yellow />
< Setter Property = BorderBrush Value = Yellow />
< / Style>
< /ComboBox.ItemContainerStyle>
< ComboBoxItem Content =一个 />
< ComboBoxItem Content = Two />
< ComboBoxItem Content =三个 />
< / ComboBox>

组合框的背景和仅下拉菜单发生变化





关于如何更改背景颜色的任何想法。

解决方案

您需要修改 ComboBox ControlTemplate c>。请参阅

很多XAML:

 < ComboBox Background = Yellow> 
< ComboBox.Resources>
< SolidColorBrush x:Key = {x:Static SystemColors.WindowBrushKey} Color = Yellow />
< SolidColorBrush x:Key = {x:Static SystemColors.HighlightBrushKey} Color = Red />
< 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.Static.Border Color =#FFACACAC />
< SolidColorBrush x:Key = ComboBox.Static.Editable.Background Color =#FFFFFFFF />
< 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 />
< LinearGradientBrush x:Key = ComboBox.MouseOver.Background EndPoint = 0,1 StartPoint = 0,0>
< GradientStop Color =#FFECF4FC Offset = 0.0 />
< GradientStop Color =#FFDCECFC Offset = 1.0 />
< / LinearGradientBrush>
< SolidColorBrush x:Key = ComboBox.MouseOver.Border Color =#FF7EB4EA />
< SolidColorBrush x:Key = ComboBox.MouseOver.Editable.Background Color =#FFFFFFFF />
< SolidColorBrush x:Key = ComboBox.MouseOver.Editable.Border Color =#FF7EB4EA />
< LinearGradientBrush x:Key = ComboBox.MouseOver.Editable.Button.Background EndPoint = 0,1 StartPoint = 0,0>
< GradientStop Color =#FFEBF4FC Offset = 0.0 />
< GradientStop Color =#FFDCECFC Offset = 1.0 />
< / LinearGradientBrush>
< SolidColorBrush x:Key = ComboBox.MouseOver.Editable.Button.Border Color =#FF7EB4EA />
< SolidColorBrush x:Key = ComboBox.Pressed.Glyph Color =#FF000000 />
< LinearGradientBrush x:Key = ComboBox.Pressed.Background EndPoint = 0,1 StartPoint = 0,0>
< GradientStop Color =#FFDAECFC Offset = 0.0 />
< GradientStop Color =#FFC4E0FC Offset = 1.0 />
< / LinearGradientBrush>
< SolidColorBrush x:Key = ComboBox.Pressed.Border Color =#FF569DE5 />
< SolidColorBrush x:Key = ComboBox.Pressed.Editable.Background Color =#FFFFFFFF />
< SolidColorBrush x:Key = ComboBox.Pressed.Editable.Border Color =#FF569DE5 />
< LinearGradientBrush x:Key = ComboBox.Pressed.Editable.Button.Background EndPoint = 0,1 StartPoint = 0,0>
< GradientStop Color =#FFDAEBFC Offset = 0.0 />
< GradientStop Color =#FFC4E0FC Offset = 1.0 />
< / LinearGradientBrush>
< 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 =#FFF0F0F0 />
< SolidColorBrush x:Key = ComboBox.Disabled.Border Color =#FFD9D9D9 />
< SolidColorBrush x:Key = ComboBox.Disabled.Editable.Background Color =#FFFFFFFF />
< 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 = ComboBoxToggleButton 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 Horizo​​ntalAlignment = 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} Horizo​​ntalAlignment = Center Margin = 0 VerticalAlignment = Center />
< / Border>
< / Border>
< ControlTemplate.Triggers>
< MultiDataTrigger>
< MultiDataTrigger.Conditions>
< Condition 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 = {绑定已启用,RelativeSource = {RelativeSource自身}} 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 = {绑定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 = {绑定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 = {绑定已启用,RelativeSource = {RelativeSource自身}} 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 = {绑定已启用,RelativeSource = {RelativeSource自身}} Value = false />
< Condition 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>

< SolidColorBrush x:Key = TextBox.Static.Background Color =#FFFFFFFF />
< /ComboBox.Resources>
< ComboBox.Template>
< ControlTemplate TargetType = {x:Type ComboBox} xmlns:Themes = clr-namespace:Microsoft.Windows.Themes; assembly = PresentationFramework.Aero2>
< 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>
<画布x:Name =画布 Horizo​​ntalAlignment = Left Height = 0 VerticalAlignment = Top Width = 0>
<矩形x:Name = opaqueRect Fill = {绑定背景,ElementName = dropDownBorder}高度= {Binding ActualHeight,ElementName = dropDownBorder}宽度= {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 =双向,RelativeSource = {RelativeSource TemplatedParent}} Style = {StaticResource ComboBoxToggleButton} />
< ContentPresenter x:Name = contentPresenter ContentTemplate = {TemplateBinding SelectionBoxItemTemplate} ContentTemplateSelector = {TemplateBinding ItemTemplateSelector} Content = {TemplateBinding SelectionBoxItem} ContentStringFormat = {TemplateBinding SelectionBoxItemStringFormat} Horizo​​ntalAlignment = { TemplateBinding Horizo​​ntalContentAlignment} 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>
<条件属性= IsGrouping值= true />
<条件属性= 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 Horizo​​ntalOffset,ElementName = DropDownScrollViewer} />
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /ComboBox.Template>
< ComboBox.ItemContainerStyle>
< Style TargetType = ComboBoxItem>
< Setter Property = Background Value = Yellow />
< Setter Property = BorderBrush Value = Yellow />
< / Style>
< /ComboBox.ItemContainerStyle>
< ComboBoxItem Content =一个 />
< ComboBoxItem Content = Two />
< ComboBoxItem Content =三个 />
< / ComboBox>


I have a WPF project where I need to change the background colour of a Combobox in XAML or c#, I have already tried using some this code but it just changes the dropdown backgrounds colour as shown by the image.

<ComboBox Background="Yellow">
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
    </ComboBox.Resources>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="BorderBrush" Value="Yellow"/>
        </Style>
    </ComboBox.ItemContainerStyle>
    <ComboBoxItem Content="One"/>
    <ComboBoxItem Content="Two"/>
    <ComboBoxItem Content="Three"/>
</ComboBox>

The background of the combobox and only the dropdown changing

Any ideas on how to change the background colour.

解决方案

You need to modify the ControlTemplate of the ComboBox. Please refer to this blog post for more information and details.

It's quite a lot of XAML:

<ComboBox Background="Yellow">
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
        <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.Static.Border" Color="#FFACACAC"/>
        <SolidColorBrush x:Key="ComboBox.Static.Editable.Background" Color="#FFFFFFFF"/>
        <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"/>
        <LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#FFECF4FC" Offset="0.0"/>
            <GradientStop Color="#FFDCECFC" Offset="1.0"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Background" Color="#FFFFFFFF"/>
        <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Border" Color="#FF7EB4EA"/>
        <LinearGradientBrush x:Key="ComboBox.MouseOver.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#FFEBF4FC" Offset="0.0"/>
            <GradientStop Color="#FFDCECFC" Offset="1.0"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="ComboBox.Pressed.Glyph" Color="#FF000000"/>
        <LinearGradientBrush x:Key="ComboBox.Pressed.Background" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#FFDAECFC" Offset="0.0"/>
            <GradientStop Color="#FFC4E0FC" Offset="1.0"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ComboBox.Pressed.Border" Color="#FF569DE5"/>
        <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Background" Color="#FFFFFFFF"/>
        <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Border" Color="#FF569DE5"/>
        <LinearGradientBrush x:Key="ComboBox.Pressed.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#FFDAEBFC" Offset="0.0"/>
            <GradientStop Color="#FFC4E0FC" Offset="1.0"/>
        </LinearGradientBrush>
        <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="#FFF0F0F0"/>
        <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>
        <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Background" Color="#FFFFFFFF"/>
        <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="ComboBoxToggleButton" 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>

        <SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF"/>
    </ComboBox.Resources>
    <ComboBox.Template>
        <ControlTemplate TargetType="{x:Type ComboBox}" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" >
            <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 ComboBoxToggleButton}"/>
                <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>
    </ComboBox.Template>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="BorderBrush" Value="Yellow"/>
        </Style>
    </ComboBox.ItemContainerStyle>
    <ComboBoxItem Content="One"/>
    <ComboBoxItem Content="Two"/>
    <ComboBoxItem Content="Three"/>
</ComboBox>

这篇关于无法更改XAML组合框的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆