Επεξεργασία

Κοινή χρήση μέσω


FrameworkElement.DefaultStyleKey Property

Definition

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

Property Value

The style key. To work correctly as part of theme style lookup, this value is expected to be the Type of the control being styled.

Examples

The following example illustrates the dependency property metadata override usage discussed in Remarks. This code defines a custom control class NumericUpDown intended to be used from a dedicated control library assembly. 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 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.

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

The complete source code for this example is available for Visual Basic.

Remarks

This property is typically not set through any of its direct property accessors. Instead, you override the type-specific metadata of this dependency property every time you create a new FrameworkElement derived class. 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.

If you want your element or control to deliberately not use theme styles, set the OverridesDefaultStyle property to true.

Dependency Property Information

Identifier field DefaultStyleKeyProperty
Metadata properties set to true AffectsMeasure

Applies to

See also