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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With