WPF框架,修改ComboBox控件背景色 ,为何如此困难?

devtools/2024/10/19 15:34:06/

直接修改Background属性不可行

修改控件背景颜色,很多人第一反应便是修改Background属性,但是修改过后便会发现,控件的颜色没有发生任何变化。
于是在网上搜索答案,便会发现一个异常尴尬的情况,要么就行代码简单但是并不管用,要么就是虽然管用,但是要添加一大堆的样式代码,而且修改的样式不仅是背景色,边框,字体,几乎所有的样式都被修改了。

我只想修改背景色这一个样式而已,就这么一个简简单单的要求,为什么要加这么多代码?
最气人的是,网上还没有单独修改背景颜色的方法。要改样式,就逼你全部一起修改。

这是wpf的模板的特性导致的,相比于修改全套的样式,保留原有样式仅仅修改背景色的话,反而要添加最多的代码,这也是为何网上目前没有仅修改背景色的方法。

控件的模板

在刚接触wpf的时候,你可能会认为这个和winform框架差不多,每个控件都是相互独立的。但是如果你了解了控件的模板,也就是ControlTemplate属性,你就会发现,wpf框架的页面布局方式,其实和html更像,绝大数控件,都是可以嵌套的。

这也是为何修改Background属性后,ComboBox的背景色没有变化。因为ComboBox.ControlTemplate属性默认值不为null,它里面自带的嵌套控件覆盖了你修改的背景颜色,所以最后展示出来,背景颜色没有任何变化。

但是,微软仅提供了设置新模板的方法,并没有提供修改原有默认模板的方法。
也就是说,要修改背景颜色,你就必须设计一套全新的模板,来替换掉原有的模板。这也是为什么网上提供的,修改背景颜色的方法,都连带着修改了所有的样式,因为从头设计模板,就意味着从头放置所有需要的嵌套控件,从头设计所有的样式。

唯一仅修改背景颜色的方式,便是将原有模板拷贝一份,在此基础上进行修改。

背景颜色修改

由于模板太大,这里建议放到单独的文件中。

<ComboBox Grid.Row="0" Template="{DynamicResource ComboBoxDiyTemplate}"><ComboBox.Resources><ResourceDictionary Source="/Style/ComboboxDiy.xaml"/></ComboBox.Resources>
</ComboBox>

然后在ComboboxDiy.xaml文件中修改背景颜色

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"><!-- 重写ComboBox的样式和模板 --><LinearGradientBrush x:Key="ComboBox.Static.Background" EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#FFF0F0F0" Offset="0.0"/><GradientStop Color="#FFE5E5E5" Offset="1.0"/></LinearGradientBrush><SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/><SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/><SolidColorBrush x:Key="ComboBox.Static.Editable.Background" Color="#FFFFFFFF"/><SolidColorBrush x:Key="ComboBox.Static.Editable.Border" Color="#FFABADB3"/><SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Background" Color="Transparent"/><SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Border" Color="Transparent"/><LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#FFECF4FC" Offset="0.0"/><GradientStop Color="#FFDCECFC" Offset="1.0"/></LinearGradientBrush><SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/><SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/><SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Background" Color="#FFFFFFFF"/><SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Border" Color="#FF7EB4EA"/><LinearGradientBrush x:Key="ComboBox.MouseOver.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#FFEBF4FC" Offset="0.0"/><GradientStop Color="#FFDCECFC" Offset="1.0"/></LinearGradientBrush><SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Border" Color="#FF7EB4EA"/><LinearGradientBrush x:Key="ComboBox.Pressed.Background" EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#FFDAECFC" Offset="0.0"/><GradientStop Color="#FFC4E0FC" Offset="1.0"/></LinearGradientBrush><SolidColorBrush x:Key="ComboBox.Pressed.Border" Color="#FF569DE5"/><SolidColorBrush x:Key="ComboBox.Pressed.Glyph" Color="#FF000000"/><SolidColorBrush x:Key="ComboBox.Pressed.Editable.Background" Color="#FFFFFFFF"/><SolidColorBrush x:Key="ComboBox.Pressed.Editable.Border" Color="#FF569DE5"/><LinearGradientBrush x:Key="ComboBox.Pressed.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#FFDAEBFC" Offset="0.0"/><GradientStop Color="#FFC4E0FC" Offset="1.0"/></LinearGradientBrush><SolidColorBrush x:Key="ComboBox.Pressed.Editable.Button.Border" Color="#FF569DE5"/><SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FFF0F0F0"/><SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/><SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/><SolidColorBrush x:Key="ComboBox.Disabled.Editable.Background" Color="#FFFFFFFF"/><SolidColorBrush x:Key="ComboBox.Disabled.Editable.Border" Color="#FFBFBFBF"/><SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Background" Color="Transparent"/><SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Border" Color="Transparent"/><Style x:Key="ComboBoxToggleButton" 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中修改背景颜色 当前案例中为白色White --><Border x:Name="templateRoot" Background="White" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true"><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="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/></Border></Border><ControlTemplate.Triggers><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/><Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=Self}}" Value="false"/><Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Mode=Self}}" Value="false"/><Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="true"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Border}"/><Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Background}"/><Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Border}"/></MultiDataTrigger><Trigger Property="IsMouseOver" Value="true"><Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.MouseOver.Glyph}"/></Trigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=Self}}" Value="true"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/></MultiDataTrigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=Self}}" Value="true"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Border}"/><Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Background}"/><Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Border}"/></MultiDataTrigger><Trigger Property="IsPressed" Value="true"><Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Pressed.Glyph}"/></Trigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Mode=Self}}" Value="true"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Border}"/></MultiDataTrigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Mode=Self}}" Value="true"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Border}"/><Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Background}"/><Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Border}"/></MultiDataTrigger><Trigger Property="IsEnabled" Value="false"><Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Disabled.Glyph}"/></Trigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="false"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Border}"/></MultiDataTrigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="false"/><Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/></MultiDataTrigger.Conditions><Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Background}"/><Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Border}"/><Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Background}"/><Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Border}"/></MultiDataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><ControlTemplate x:Key="ComboBoxDiyTemplate" 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 Mode=TemplatedParent}}" Margin="1" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"><theme:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}"><Border x:Name="dropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1"><ScrollViewer x:Name="DropDownScrollViewer"><Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled"><Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"><Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/></Canvas><ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/></Grid></ScrollViewer></Border></theme:SystemDropShadowChrome></Popup><ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/><ContentPresenter x:Name="contentPresenter" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Grid><ControlTemplate.Triggers><Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true"><Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/><Setter Property="Color" TargetName="shadow" Value="#71000000"/></Trigger><Trigger Property="HasItems" Value="false"><Setter Property="Height" TargetName="dropDownBorder" Value="95"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsGrouping" Value="true"/><Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/></MultiTrigger.Conditions><Setter Property="ScrollViewer.CanContentScroll" Value="false"/></MultiTrigger><Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false"><Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/><Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/></Trigger></ControlTemplate.Triggers></ControlTemplate>
</ResourceDictionary>

http://www.ppmy.cn/devtools/49607.html

相关文章

算法刷题【二分法】

题目&#xff1a; 注意题目中说明了数据时非递减的&#xff0c;那么这样就存在二分性&#xff0c;能够实现logn的复杂度。二分法每次只能取寻找特定的某一个值&#xff0c;所以我们要分别求左端点和有端点。 分析第一组用例得到结果如下: 成功找到左端点8 由此可知&#xff0…

linux centos consul1.15.2一键安装部署

consul原理、作用、安装相关内容 一、理论部分二、安装下载版本地址三、安装consul服务 一、理论部分 1、consul的原理 Consul的原理及作用可以归纳为以下几点&#xff1a; ①、基于Gossip协议的通信&#xff1a;Consul使用了基于Gossip协议的Serf实现来进行通信。 Gossip协议…

Hudi extraMetadata 研究总结

前言 研究总结 Hudi extraMetadata ,记录研究过程。主要目的是通过 extraMetadata 保存 source 表的 commitTime (checkpoint), 来实现增量读Hudi表写Hudi表时,保存增量读状态的事务性,实现类似于流任务中的 exactly-once 背景需求 有个需求:增量读Hudi表关联其他Hudi…

Linux构建本地时间同步ntp

环境介绍&#xff1a; 主机名 IP地址 系统发行版 环境 Node01 192.168.100.102 Centos 7.4 可联网、已关闭防火墙selinux Node02 192.168.100.103 Centos 7.4 已关闭防火墙selinux 1.主节点同步阿里云标准时间 在保证连接外网的情况下&#xff0c;同步阿里服务器的…

PHP发送邮件的SMTP配置教程?有哪些技巧?

PHP发送邮件功能如何实现&#xff1f;怎么确保PHP发信的安全性&#xff1f; 在Web开发中&#xff0c;使用PHP发送电子邮件是一项常见且重要的任务。SMTP是PHP用于发送邮件的标准协议之一。AokSend将详细介绍如何配置PHP以通过SMTP服务器发送电子邮件。 PHP发送邮件&#xff1…

三篇卫星切换的论文

目录 一、Energy-Aware Satellite Handover based on Deep Reinforcement Learning 1、题目翻译 2、来源 3、内容 二、A Reliable Handover Strategy with Second Satellite Selection in LEO Satellite Networks 1、题目翻译 2、来源 3、内容 三、User Grouping-Based…

selenium和urllib3版本冲突解决

问题 使用selenium的时候控制台报错 ValueError: Timeout value connect was <object object at 0x000002610094BEC0>, but it must be an int, float or None.根因 根因是版本冲突 Name冲突版本合理版本selenium3.141.04.21.0urllib32.0.32.2.1 解决 解决方法是把…

曲线拟合 | 二次B样条拟合曲线

B 样条曲线拟合实例&#xff1a;能平滑化曲线 1. 实例1 为MASS包中mcycle数据集。它测试了一系列模拟的交通车事故中&#xff0c;头部的加速度&#xff0c;以此来评估头盔的性能。times为撞击时间(ms)&#xff0c;accel为加速度&#xff08;g&#xff09;。首先导入数据&#…