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 是要從專用控制項程式庫元件使用。 說明的靜態建構函式會參考一些私用初始化函式, (另一個常見的控制項子類別化案例註冊類別處理常式;請參閱 將路由事件標示為已處理,以及類別處理) ,最後覆寫 DefaultStyleKey 類別上的 NumericUpDown 相依性屬性中繼資料。 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

適用於

另請參閱