更改组合框的背景颜色,它根本不改变颜色

Posted

技术标签:

【中文标题】更改组合框的背景颜色,它根本不改变颜色【英文标题】:Changing background color for a ComboBox, it's not changing color at all 【发布时间】:2016-07-31 02:00:38 【问题描述】:

我疯了,我就是不能改变 ComboBox 的颜色。已尝试在 ComboBox 上使用背景属性,但没有任何反应。

也尝试使用样式块并设置背景颜色,但这也不起作用。

代码

<ComboBox Padding="7" Height="34" Background="#ffffff">
            <ComboBox.Resources>
                <Style x:Key="x:Type ComboBox" TargetType="ComboBox">
                    <Setter Property="Background" Value="red" />
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="BorderBrush" Value="black" />
                </Style>
            </ComboBox.Resources>
            <ComboBoxItem IsSelected="True">1 - Room</ComboBoxItem>
            <ComboBoxItem>2 - Rooms</ComboBoxItem>
            <ComboBoxItem>3 - Rooms</ComboBoxItem>
            <ComboBoxItem>4 - Rooms</ComboBoxItem>
            <ComboBoxItem>5+ - Rooms</ComboBoxItem>
        </ComboBox>

即使我将背景颜色设置为白色,它仍然只是标准的灰色。

在这里你可以看到它的样子:

希望有人能告诉我我做错了什么?

【问题讨论】:

看看这个answer。看起来您必须实现自己的 ControlTemplate。 【参考方案1】:

我认为以下几点可以帮助您:

    从 ComboBox 声明中删除背景定义 (Background="#ffffff")。 将组合项声明移动到组合保存 Grid,因为 ItemTemplate 和 ItemTemplateSelector 对于 ItemsControl 的容器中已有的项被忽略。 实现数据模板选择器,支持combo的数据模板(一为选中项,二为待选项)。

这是 XAML 代码

 <Grid>
    <Grid.Resources>
        <x:Array Type="x:Type system:String" x:Key="MyRoomsArray">
            <system:String>1 - Room</system:String>
            <system:String>2 - Rooms</system:String>
            <system:String>3 - Rooms</system:String>
            <system:String>4 - Rooms</system:String>
            <system:String>5+ - Rooms</system:String>
        </x:Array>
    </Grid.Resources>
    <ComboBox Padding="7" Height="34" SelectedIndex="0" ItemsSource="StaticResource MyRoomsArray">
        <ComboBox.Resources>
            <DataTemplate x:Key="ItemToSelect">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                            Background="Red" 
                            BorderBrush="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=ComboBox, Path=BorderBrush, UpdateSourceTrigger=PropertyChanged"  
                            BorderThickness ="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=ComboBox, Path=BorderThickness, UpdateSourceTrigger=PropertyChanged">
                        <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="Binding " />
                    </Border>
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="SelectedItem">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                            Background="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=ComboBox, Path=Background, UpdateSourceTrigger=PropertyChanged" 
                            BorderBrush="Transparent"  
                            BorderThickness ="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=ComboBox, Path=BorderThickness, UpdateSourceTrigger=PropertyChanged">
                        <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="Binding " />
                    </Border>
                </Grid>
            </DataTemplate>
            <wpfComboBAckground:ComboDataTemplateSelector x:Key="ComboDataTemplateSelector" Selected="StaticResource SelectedItem" ItemToSelect="StaticResource ItemToSelect"/>
            <Style TargetType="ComboBox">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Background" Value="Red" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="BorderBrush" Value="Black" />
                <Setter Property="ItemTemplateSelector" Value="StaticResource ComboDataTemplateSelector"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Transparent"></Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Background" Value="Red"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Resources>
    </ComboBox>
</Grid>

这是数据模板选择器

public class ComboDataTemplateSelector : DataTemplateSelector

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    
        var selected = false;
        // container is the ContentPresenter
        FrameworkElement fe = container as FrameworkElement;
        if (fe == null) return ItemToSelect;
        var cbo = fe.TemplatedParent as ComboBox;

        if (cbo != null)
            selected = true;

        return selected ? Selected : ItemToSelect;
    

    public DataTemplate Selected  get; set; 

    public DataTemplate ItemToSelect  get; set; 

外观:

问候。

【讨论】:

以上是关于更改组合框的背景颜色,它根本不改变颜色的主要内容,如果未能解决你的问题,请参考以下文章

WPF comboboxitem 前景色改变

MFC:如何更改整个编辑框的背景颜色?

如何用js更改tr的背景颜色 新手求助

WPF 组合框禁用背景颜色

更改 jquery mobile listview 背景颜色

pygtk 更改 gtkHBox 小部件的背景颜色