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. This is a dependency property.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

protected internal Object DefaultStyleKey { get; set; }
/** @property */
protected Object get_DefaultStyleKey ()

/** @property */
protected void set_DefaultStyleKey (Object value)

protected internal function get DefaultStyleKey () : Object

protected internal function set DefaultStyleKey (value : Object)

You cannot use this property in XAML.

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.

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

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

...

}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft