You can define a style inline in the XAML for a control, or as a reusable resource.

Defining styles as a reusable resource

Style definition locations

An individual page’s XAML file (will override a same named resource in App.xaml)

Will override a same named resource in App.xaml if present.

In the App.xaml file

In a separate resource dictionary XAML file

Can be shared across apps, and more than one resource dictionary can be merged in a single app).

Scope is determined by where the resource dictionary is referenced.

Style definitions

TargetType attribute

String that specifies a FrameworkElement type to apply the style to.

Must specify a FrameworkElement-derived type that’s defined by the Windows Runtime or a custom type that’s available in a referenced assembly.

If you try to apply a style to a control and the control’s type doesn’t match the TargetType attribute of the style you’re trying to apply, an exception occurs.

Setter elements

A collection of one or more Setter elements.  Each requires a Property and a Value.

x:Key

If a style contains the x:Key attribute, you can only apply it to a control by setting the Style property of the control to the keyed style. A style without an x:Key attribute is automatically applied to every control of its target type, that doesn’t otherwise have an explicit style setting.

Inheriting from other styles

Use the BasedOn property

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *