FrameworkElement.DefaultStyleKey FrameworkElement.DefaultStyleKey FrameworkElement.DefaultStyleKey FrameworkElement.DefaultStyleKey Property

定义

获取或设置在使用或定义主题样式时要用于引用此控件样式的键。Gets or sets the key to use to reference the style for this control, when theme styles are used or defined.

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

属性值

样式键。The style key. 要作为主题样式查找的一部分正常工作,此值应是带样式的控件的 TypeTo work correctly as part of theme style lookup, this value is expected to be the Type of the control being styled.

示例

下面的示例演示了备注中讨论的依赖属性元数据覆盖用法。The following example illustrates the dependency property metadata override usage discussed in Remarks. 此代码定义了一个自定义NumericUpDown控件类, 该类旨在用于专用的控件库程序集。This code defines a custom control class NumericUpDown intended to be used from a dedicated control library assembly. 阐释的静态构造函数引用了某些专用初始化函数、注册类处理程序 (另一常见控件子类方案; 请参阅将路由事件标记为已处理, 以及类处理) 并最终覆盖DefaultStyleKey类上的NumericUpDown依赖项属性元数据。The illustrated static constructor references some private initialization function, registers a class handler (another common control subclassing scenario; see Marking Routed Events as Handled, and Class Handling) and finally overrides the DefaultStyleKey dependency property metadata on the NumericUpDown class. DefaultStyleKey始终将其自己的类型作为预期键返回, 这是主题样式系统用于查找某些任意非样式控件的样式的约定。DefaultStyleKey always returns its own type as the intended key, which is the convention that the theme style system uses to look up the style for some arbitrary otherwise non-styled control. 完整示例还定义了该键引用的实际控件主题样式;请参阅NumericUpDown 自定义控件以及主题和 UI 自动化支持示例The complete sample also defines the actual control's theme style that is referenced by that key; see NumericUpDown Custom Control with Theme and UI Automation Support Sample.

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

GitHub 上提供了此示例的完整源代码C# , 适用于和Visual BasicThe complete source code for this example is available on GitHub for both C# and Visual Basic.

注解

通常不通过其任何直接属性访问器设置此属性。This property is typically not set through any of its direct property accessors. 相反, 每次创建新FrameworkElement的派生类时, 都要重写此依赖项属性的特定于类型的元数据。Instead, you override the type-specific metadata of this dependency property every time you create a new FrameworkElement derived class. 派生控件时, 在控件派生类OverrideMetadata (或等效DefaultStyleKeyProperty类初始化) 的静态构造函数中, 针对标识符调用方法。When you derive a control, call the OverrideMetadata method against the DefaultStyleKeyProperty identifier, within the static constructor of the control derived class (or equivalent class initialization).

控件通常会将此属性的默认值覆盖为其自己的类型, 但在某些情况下, 可能还会使用主题字典中的样式所在的基类型。A control typically overrides the default value of this property to be its own type, but in some cases could also use a base type for which a style in the theme dictionaries exists. 这仅适用于基本控件的控件模板完全定义该派生控件的可视化表示形式, 并且如果派生类型公开的任何其他成员不需要作为控件模板的一部分的其他元素, 这是可行的。This is only practical if the control templates of the base control entirely define the visual representation of that derived control, and if whatever additional members the derived types expose do not require additional elements as part of the control template.

如果希望你的元素或控件特意不使用主题样式, 请将OverridesDefaultStyle属性设置为。 trueIf you want your element or control to deliberately not use theme styles, set the OverridesDefaultStyle property to true.

依赖项属性信息Dependency Property Information

标识符字段Identifier field DefaultStyleKeyProperty
元数据属性设置为trueMetadata properties set to true AffectsMeasure

适用于

另请参阅