Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between approches for setting DefaultStyleKey

I am creating a custom control(deriving from Control) and want to define a default theme for the control. Previously all the custom controls I created, I have used

static IntegerUpDown()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(IntegerUpDown), 
    new FrameworkPropertyMetadata(typeof(IntegerUpDown)));
}

with this assembly attribute:

[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly,
ResourceDictionaryLocation.SourceAssembly)]

Alternative approach to do this is (which I have also noticed in some controls) -

public IntegerUpDown()
{
    DefaultStyleKey = typeof(IntegerUpDown);
}

I would like to know the pros and cons of these two approaches and which one to prefer?

like image 801
akjoshi Avatar asked Oct 04 '11 10:10

akjoshi


People also ask

What is a default style?

The default style is a group of settings that control the appearance of a chart item: Font. Entity or link type. Display options, such as which items properties are shown on the chart.

How do I set styles in XAML?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

What is FocusVisualStyle?

A FocusVisualStyle is additive to any control template style that comes either from an explicit style or a theme style; the primary style for a control can still be created by using a ControlTemplate and setting that style to the Style property.


1 Answers

I can observe that the first approach asks the dependency property framework to register a default style key. It does that only once (being in a static constructor) and then onwards it is used for all instances of IntegerUpDown. Second approach assignes the Key explicitly when an instance of IntegerUpDown is created on its own. They both seem ok to me.

MSDN says ...

Metadata can be overriden so that subclasses can tweak a DP by overriding the property’s metadata, instead of completely re-implementing the property itself.

like image 174
WPF-it Avatar answered Oct 21 '22 15:10

WPF-it