WPF TextBox和ComboBox处于禁用状态(背景颜色看起来不一样)

时间:2017-03-13 22:16:15

标签: wpf combobox background textbox styles

以下是我的控件被禁用时的图像:

enter image description here

正如你所看到的那样,即使我使用相同的颜色等,组合框与文本框完全不同,我也不知道如何才能使它们看起来相同?

以下是我的文本框样式的代码:

<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="#E0E4E5" />
            <Setter Property="BorderBrush" Value="#E0E4E5" />
            <Setter Property="BorderThickness" Value="1.5" />
        </Trigger>
    </Style.Triggers>

    <Setter Property="BorderBrush" Value="#0091EA"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这里是我的COMBOBOX的代码:

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
      <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="#E0E4E5" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="True">
                        <Setter Property="Background" Value="Transparent" />
                    </Trigger>
                </Style.Triggers>
     </Style>

在禁用它们时,我需要做些什么来使它们看起来一样?不知何故,颜色看起来不应该是......

我还尝试将模板设置为我的组合框样式,但我没有丢失箭头,应用此选项后,它看起来比ComboBox更像TextBox:

<Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

1 个答案:

答案 0 :(得分:1)

您可以尝试覆盖SystemColors.ControlBrushKey画笔:

<ComboBox>
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#E0E4E5" />
    </ComboBox.Resources>
...
</ComboBox>

这应该适用于Windows 7.在Windows 8及更高版本中,您需要覆盖ControlTemplate的整个ComboBox。您可以在Visual Studio 2012及更高版本中以设计模式右键单击ComboBox元素,然后选择编辑模板 - >编辑副本将默认模板复制到XAML标记中,然后根据您的要求进行编辑。在IsEnabledStyle中查找ControlTemplate触发器。

有关详细信息,请参阅以下博文:https://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/