Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.DefaultStyleKey Property

Gets or sets the key to use to reference the style for this control, when theme styles are used or defined.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

protected internal Object DefaultStyleKey { get; set; }

Property Value

Type: System.Object
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.

Identifier field

DefaultStyleKeyProperty

Metadata properties set to true

AffectsMeasure

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.

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. 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.


public 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)));
    }


...


}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

Show:
© 2014 Microsoft