样式错误?无法在Windows10的样式内设置背景色

时间:2019-05-10 12:26:08

标签: wpf combobox windows-10

当我尝试为ComboBox设置BackgroundColor时,它在Windows7中可以正常工作,但在Windows 10中则不能。这是预期的行为还是错误? 编辑:我不是说ComboBox弹出框,只是普通ComboBox的背景色。

<ComboBox IsEditable="True">
        <ComboBox.Style>
            <Style TargetType="ComboBox">
            <Setter Property="Background" Value="Yellow"></Setter>                 
            </Style>
        </ComboBox.Style>            
  </ComboBox>

编辑:当我使用此代码时,只是为了使所有注释正确:

 <ComboBox IsEditable="True" Background="Yellow"/>

一切正常,组合框为黄色。但是,当我想在样式中执行相同操作时-我应该期望它不起作用,因为它的意图是?说真的这是Styles的目的!

Windows 10

Windows 7

1 个答案:

答案 0 :(得分:5)

如果您编辑默认模板:

EditACopy

您会发现您所指的ToggleButtonBorder(通过ControlTemplate设置)和硬编码的LinearGradientBrush组成,由ControlTemplate的触发器更改。

<ControlTemplate x:Key="ComboBoxControlTemplate" 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">

            <!--Left out for brevity-->

        </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}}">
            <ToggleButton.Style>
                <Style 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="#FFACACAC" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                                    <Border.Background>
                                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                <GradientStop Color="#FFF0F0F0" Offset="0"/>
                                                <GradientStop Color="#FFE5E5E5" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                    <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="F1M0,0L2.667,2.66665 5.3334,0 5.3334,-1.78168 2.6667,0.88501 0,-1.78168 0,0z" Fill="#FF606060" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                                    </Border>
                                </Border>
                                <ControlTemplate.Triggers>

                                    <!--Left out for brevity-->

                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Grid>
    <ControlTemplate.Triggers>

        <!--Left out for brevity-->

    </ControlTemplate.Triggers>
</ControlTemplate>

要启用样式,可以将BorderBrushBackground属性通过Border转发到TemplateBinding

<ControlTemplate TargetType="{x:Type ToggleButton}">
    <Border x:Name="templateRoot" 
            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
            Background="{TemplateBinding Background}" SnapsToDevicePixels="True">

        <!--
        <Border.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF0F0F0" Offset="0"/>
                <GradientStop Color="#FFE5E5E5" Offset="1"/>
            </LinearGradientBrush>
        </Border.Background>
        -->

        <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="F1M0,0L2.667,2.66665 5.3334,0 5.3334,-1.78168 2.6667,0.88501 0,-1.78168 0,0z" Fill="#FF606060" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
        </Border>
    </Border>
    <ControlTemplate.Triggers>

        <!--Left out for brevity-->

    </ControlTemplate.Triggers>
</ControlTemplate>