FrameworkElement.DefaultStyleKey 屬性

定義

取得或設定可在使用或定義佈景主題樣式時,用來參考這個控制項之樣式的索引鍵。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

屬性值

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 屬性中繼資料 NumericUpDownThe 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 具有主題的自訂控制項和消費者介面自動化支援範例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 Basic中取得。The 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. 相反地,您會在每次建立新的衍生類別時,覆寫這個相依性屬性的特定類型中繼資料 FrameworkElementInstead, 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.

如果您希望元素或控制項刻意不使用主題樣式,請將屬性設定 OverridesDefaultStyletrueIf 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

適用於

另請參閱