建站行业是什么意思,wordpress中文开发文档下载,网站优化需要那些工具,jq 网站头部广告代码一、目的#xff1a;介绍下在WPF中OverridesDefaultStyle属性如何使用 OverridesDefaultStyle 属性在 WPF 中用于控制控件是否使用默认的主题样式。将其设置为 True 时#xff0c;控件将不会应用默认的主题样式#xff0c;而是完全依赖于你在 Style 中定义的样式。以下是如何…一、目的介绍下在WPF中OverridesDefaultStyle属性如何使用 OverridesDefaultStyle 属性在 WPF 中用于控制控件是否使用默认的主题样式。将其设置为 True 时控件将不会应用默认的主题样式而是完全依赖于你在 Style 中定义的样式。以下是如何使用 OverridesDefaultStyle 属性的详细说明和示例。
二、使用场景 • 完全自定义控件样式当你希望完全自定义控件的外观而不希望继承任何默认样式时可以将 OverridesDefaultStyle 设置为 True。 • 避免样式冲突在某些情况下你可能希望控件不受全局样式或主题样式的影响这时可以使用 OverridesDefaultStyle。 三、示例
以下是一个示例展示了如何使用 OverridesDefaultStyle 属性 完全自定义按钮样式 Button ContentButtonButton.StyleStyle TargetTypeButton!-- Set to true to not get any properties from the themes. --Setter PropertyOverridesDefaultStyle ValueTrue /Setter PropertyTemplateSetter.ValueControlTemplate TargetTypeButtonGridEllipse Fill{TemplateBinding Background} /ContentPresenter HorizontalAlignmentCenterVerticalAlignmentCenter //Grid/ControlTemplate/Setter.Value/Setter/Style/Button.Style/Button
可以看到对比效果 从上面的例子可以看到设置OverridesDefaultStyleTrue的Button样式Background是null没有从默认的Button样式中继承这个就是OverridesDefaultStyle的主要作用可以完全控制控件样式而不受默认样式的干扰。 同DefaultStyleKeyProperty的区别 在 WPF 中OverridesDefaultStyle 和 DefaultStyleKeyProperty 是两个不同的属性它们在自定义控件的样式和行为方面起着不同的作用。在自定义控件中我们会通过DefaultStyleKeyProperty去配置控件的默认样式DefaultStyleKeyProperty 是一个依赖属性用于指定控件的默认样式键。通过重写 DefaultStyleKeyProperty 的元数据你可以为自定义控件指定一个默认样式。通常在自定义控件的静态构造函数中使用 DefaultStyleKeyProperty.OverrideMetadata 方法来设置。如 public class CustomControl1 : Control{static CustomControl1(){DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));}}
在 Generic.xaml 中定义样式
ResourceDictionaryxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfCustomControlLibrary1Style TargetType{x:Type local:CustomControl1}Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type local:CustomControl1}Border BackgroundLightGray BorderBrushBlack BorderThickness1ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate/Setter.Value/Setter/Style
/ResourceDictionary • OverridesDefaultStyle用于指示控件是否应忽略默认的主题样式。设置为 True 时控件将完全依赖于你在 Style 中定义的样式。 • DefaultStyleKeyProperty用于指定控件的默认样式键。通过重写 DefaultStyleKeyProperty 的元数据你可以为自定义控件指定一个默认样式。 这两个属性在自定义控件的样式和行为方面起着不同的作用通常在自定义控件的开发中会结合使用以实现所需的样式和行为。 需要了解的知识点
FrameworkElement.OverridesDefaultStyle 属性 (System.Windows) | Microsoft Learn
FrameworkElement.DefaultStyleKeyProperty Field (System.Windows) | Microsoft Learn
System.Windows.Controls 命名空间 | Microsoft Learn控件自定义 - WPF .NET Framework | Microsoft Learn
控件库 - WPF .NET Framework | Microsoft Learn
源码地址
GitHub - HeBianGu/WPF-ControlDemo: 示例
GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库
GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库
了解更多
System.Windows.Controls 命名空间 | Microsoft Learn
https://github.com/HeBianGu
HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频