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 internal object DefaultStyleKey { get; set; }

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

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

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also