笔记:在WPF中OverridesDefaultStyle属性如何使用

一、目的:介绍下在WPF中OverridesDefaultStyle属性如何使用

OverridesDefaultStyle 属性在 WPF 中用于控制控件是否使用默认的主题样式。将其设置为 True 时,控件将不会应用默认的主题样式,而是完全依赖于你在 Style 中定义的样式。以下是如何使用 OverridesDefaultStyle 属性的详细说明和示例。

二、使用场景

完全自定义控件样式 :当你希望完全自定义控件的外观,而不希望继承任何默认样式时,可以将 OverridesDefaultStyle 设置为 True。

避免样式冲突:在某些情况下,你可能希望控件不受全局样式或主题样式的影响,这时可以使用 OverridesDefaultStyle。

三、示例

以下是一个示例,展示了如何使用 OverridesDefaultStyle 属性:

完全自定义按钮样式

XML 复制代码
 <Button Content="Button">
     <Button.Style>
         <Style TargetType="Button">
             <!--  Set to true to not get any properties from the themes.  -->
             <Setter Property="OverridesDefaultStyle" Value="True" />
             <Setter Property="Template">
                 <Setter.Value>
                     <ControlTemplate TargetType="Button">
                         <Grid>
                             <Ellipse Fill="{TemplateBinding Background}" />
                             <ContentPresenter HorizontalAlignment="Center"
                                               VerticalAlignment="Center" />
                         </Grid>
                     </ControlTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
     </Button.Style>
 </Button>

可以看到对比效果

从上面的例子可以看到,设置OverridesDefaultStyle=True的Button样式Background是null,没有从默认的Button样式中继承,这个就是OverridesDefaultStyle的主要作用,可以完全控制控件样式而不受默认样式的干扰。

同DefaultStyleKeyProperty的区别

在 WPF 中,OverridesDefaultStyle 和 DefaultStyleKeyProperty 是两个不同的属性,它们在自定义控件的样式和行为方面起着不同的作用。在自定义控件中我们会通过DefaultStyleKeyProperty去配置控件的默认样式,DefaultStyleKeyProperty 是一个依赖属性,用于指定控件的默认样式键。通过重写 DefaultStyleKeyProperty 的元数据,你可以为自定义控件指定一个默认样式。通常在自定义控件的静态构造函数中使用 DefaultStyleKeyProperty.OverrideMetadata 方法来设置。如:

XML 复制代码
    public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }

在 Generic.xaml 中定义样式

XML 复制代码
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
    
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="LightGray" BorderBrush="Black" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </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个人主页-哔哩哔哩视频

相关推荐
samble2 天前
从零搭建工业控制系统(二十二):线程安全与并发控制
c#·wpf·并发·mvvm·线程安全·工业控制
姜穆澜2 天前
OneID 从 0 到 1 完整生产案例(三)
大数据·wpf
dalong103 天前
WPF:MVVM 示例
大数据·wpf
samble3 天前
从零搭建工业控制系统(二十一):状态管理系统——中心状态对象设计
c#·wpf·mvvm·状态管理·工业控制
mengge.cloud3 天前
存储技术基础小白教程
linux·运维·服务器·wpf·存储
DreamLife☼4 天前
复杂Agent系统架构设计
系统架构·wpf·agent·组件·设计·构架·说明
SamChan904 天前
PDF翻译服务端到端基准测试:4款主流方案的吞吐量、延迟、内存占用对比
服务器·网络·python·ai·pdf·wpf
bugcome_com4 天前
Avalonia 控件模板实战:从 WPF 迁移自定义 Button 样式的完整指南
wpf
七夜zippoe4 天前
DolphinDB 故障排查实战:系统、数据库与集群常见问题诊断
数据库·wpf·集群·常见问题·故障排查·dolphindb
dalong105 天前
WPF:箭头绘制程序
wpf·自定义绘制