Changes

XAML Best practices

449 bytes added, 24 June
/* Implicit and Explicit Styling */
=== Implicit and Explicit Styling ===
A Style is a collection of preset attributes that can be assigned to a XAML control of the same type.
When this is done directly, by adding the Style attribute to a control, it is called Explicit Styling.
However, styles can also be made Implicit, by not assigning an x:Key, making it a default style. Whether it is the default style for a localized section of a XAML, or the entire project, depends on where the Implicit style is defined.
 
The following code snippet makes use of several features of XAML Styling:
[[File:Performance_ImplicitStyling.jpg]]
* You can use very The above is an example of a localized ResourceDictionaries ResourceDictionary used to avoid repeatedly customizing similar controls. In this example, all Labels within the StackPanel will inherit the defined style.* By not assigning an x:Key to the style, it will be used as the default Implicit style for all controls of the Label type Labels within that StackPanel unless a Style assigned to them. This is called '''Implicit Styling'''.* On the other hand, Labels with a Style an Explicit style attribute will completely ignore any inherited Implicit styling.* The If no Explicit style is assigned, the Implicit style will be assigned first, and will be overwritten by any defined attributes, like TextColor in this example.
* The localized Style definitions in this StackPanel will be passed down to all controls contained within it, no matter how deep the nesting goes.
* The BasedOn attribute can be used to create a sub-style based on another existing style. However, the TargetType must always match.
487
edits