In WPF, a style is also a set of properties applied to content used for visual rendering. A style can be used to set properties on an existing visual element, such as setting the font weight of a Button control, setting of the background color of the TextBox control or it can be used to define the way an object looks, such as showing the name and age from a Person object. In addition to the features in word processing styles, WPF styles have specific features for building applications, including the ability to associate different visual effects based on user events, provide entirely new looks for existing controls, and even designate rendering behavior for non-visual objects. All of these features come without the need to build a custom control.
In other words the function of the Style is very similar to the .CSS in web application programming.
These are the following different types of Style in WPF
- Inline Style
- Named Style
- Element typed Style
- Data Template and Style
- Control Template
As an example of how styles can make themselves useful in WPF, let’s take a simple implementation of Button control.
WPF Inline Style
Each and every "style-able" element in WPF has a Style property, which can be set inline using standard XAML property-element syntax. For setting the Style property of an element we use the Style and Setter. Trough Setter tag we set the value of the element properties .
Let's take an example of the Button

Because we want to bundle three property values into our style, we have a Style element with three Setter sub-elements, one for each property we want to set—i.e., FontSize , FontWeight and Background—those with the Button prefix to indicate the class that contains the property. Properties suitable for styling are dependency properties.
Limitation With Inline Style
Inline styles can’t be shared across elements, inline styles actually involve more typing than just setting the properties. For this reason, inline styles aren’t used nearly as often as named styles.
Named Style in WPF
By publishing the same inline style into a resource, and giving a name, then we can use it by its name in our element instance.
Note:- To use the Named Style we use the StaticResource element of the style attribute
No comments:
Post a Comment