WPF TextBox和ComboBox处于禁用状态(背景颜色看起来不一样)(WPF TextBox and ComboBox in disabled state (background color

编程入门 行业动态 更新时间:2024-10-23 06:27:24
WPF TextBox和ComboBox处于禁用状态(背景颜色看起来不一样)(WPF TextBox and ComboBox in disabled state (background color does not look the same))

这是我的控件禁用时的图像:

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

这是我的文本框样式的代码:

<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>

Here is image of my controls when they are disabled:

As you can see combobox is pretty much different that textbox even if I use same colours etc, I don't know how could I achieve to make them look same?

Here is code of my textbox style:

<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>

AND HERE IS CODE OF MY 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>

What do I need to do to make them look the same when they are disabled? Somehow colour is not looking as it should be ..

I tried also setting template to my combobox style but than I'm loosing arrow and it look more TextBox than a ComboBox after applying this:

<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>

最满意答案

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

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

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

有关此内容的更多信息,请参阅以下博客文章: https : //blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows -8 /

You could try to override the SystemColors.ControlBrushKey brush:

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

This should work on Windows 7. On Windows 8 and later you need to override the entire ControlTemplate of the ComboBox. You can right-click on the ComboBox element in design mode in Visual Studio 2012 and later and choose Edit Template->Edit a Copy to copy the default template into your XAML markup and then edit it as per your requirements. Look for the IsEnabled triggers in the Style or the ControlTemplate.

Please refer to the following blog post for more information about this: https://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/

更多推荐

<Setter,Property,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-04-28 01:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   状态   背景   ComboBox   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!