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()

            // Listen to MouseLeftButtonDown event to determine if slide should move focus to itself
                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()

			' 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