FrameworkElement.DefaultStyleKey 属性

定义

获取或设置在使用或定义主题样式时要用于引用此控件样式的键。

protected public:
 property System::Object ^ DefaultStyleKey { System::Object ^ get(); void set(System::Object ^ value); };
protected internal object DefaultStyleKey { get; set; }
member this.DefaultStyleKey : obj with get, set
Protected Friend Property DefaultStyleKey As Object

属性值

样式键。 要作为主题样式查找的一部分正常工作,此值应是带样式的控件的 Type

示例

以下示例说明了“备注”中讨论的依赖属性元数据替代用法。 此代码定义旨在从专用控件库程序集使用的自定义控件类 NumericUpDown 。 演示的静态构造函数引用一些私有初始化函数, (另一个常见的控件子类化方案注册类处理程序;请参阅将路由事件标记为已处理和类处理) 最后重写类上的DefaultStyleKeyNumericUpDown依赖属性元数据。 DefaultStyleKey 始终返回自己的类型作为预期键,这是主题样式系统用于查找某个任意非样式控件的样式的约定。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Automation;
using System.Globalization;
using System.Diagnostics;

namespace CustomControlLibrary
{
    public partial class NumericUpDown : Control
    {
        static NumericUpDown()
        {
            InitializeCommands();

            // Listen to MouseLeftButtonDown event to determine if slide should move focus to itself
            EventManager.RegisterClassHandler(typeof(NumericUpDown), 
                Mouse.MouseDownEvent, new MouseButtonEventHandler(NumericUpDown.OnMouseLeftButtonDown), true);

            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown)));
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input

Namespace CustomControlLibrary
    Public Partial Class NumericUpDown
        Inherits Control

        Shared Sub New()
            InitializeCommands()

            ' Listen to MouseLeftButtonDown event to determine if slide should move focus to itself
            EventManager.RegisterClassHandler(GetType(NumericUpDown), Mouse.MouseDownEvent, New MouseButtonEventHandler(AddressOf NumericUpDown.OnMouseLeftButtonDown), True)

            DefaultStyleKeyProperty.OverrideMetadata(GetType(NumericUpDown), New FrameworkPropertyMetadata(GetType(NumericUpDown)))
        End Sub
    End Class
End Namespace

此示例的完整源代码适用于 Visual Basic

注解

此属性通常不是通过其任何直接属性访问器设置的。 而是在每次创建新的 FrameworkElement 派生类时重写此依赖属性的类型特定的元数据。 派生控件时,请在控件派生类的静态构造函数 (或等效类初始化) 中针对DefaultStyleKeyProperty标识符调用 OverrideMetadata 方法。

控件通常将此属性的默认值重写为其自己的类型,但在某些情况下,还可以使用主题字典中存在样式的基类型。 仅当基控件的控件模板完全定义该派生控件的可视表示形式,并且派生类型公开的任何附加成员不需要其他元素作为控件模板的一部分时,这才可行。

如果希望元素或控件故意不使用主题样式,请将 属性 OverridesDefaultStyle 设置为 true

依赖项属性信息

标识符字段 DefaultStyleKeyProperty
元数据属性设置为 true AffectsMeasure

适用于

另请参阅