更改ComboBox的背景颜色,它根本不会改变颜色

时间:2016-04-09 21:09:37

标签: wpf xaml combobox

我发疯了,我无法改变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>

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

在这里你可以看到它的外观:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

以下是我认为可以帮助你的几件事:

  1. 从ComboBox声明中删除背景定义(背景=&#34; #ffffff&#34;)。
  2. 将组合项声明移动到组合保持网格,因为ItemConmplate和ItemTemplateSelector已被ItemsControl容器的项目忽略。
  3. 实施数据模板选择器以支持组合的数据模板(一个用于所选项目,第二个用于要选择的项目)。
  4. 以下是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; }
    }
    

    看起来如何: here

    问候。