Wpf Unleashed 4

Download .zip Download .tar.gz View on GitHub

Creating a Style

For this example, lets build a Style for a Button! The syntax is as following:

<Style x:Key="styleKey" TargetType="{x:Type Button}">
	....
	<Setter Property="Background" Value="Black" />
	<Setter Property="Foreground" Value="White" />
	....
</Style>

The style has a key for referencing, and its optional to specify a TargetType. What this does, if specified, is to limit the type of controls that can use this style. For this example, lets restrict this to Buttons only.
Another optional property, is BasedOn="", which if specified, allows us to inherit behavior from another style!.

In the body of the style, we can have has many Setters as we need, specifing the property it affects, and their desired value.

With this simple style, if we have a button in out XAML, by simply setting its Style to ="{StaticResource styleKey}", we have our style set to the button!

Template

For advanced customization of our Controls, we can override the default template! An example of this can be seen in the code Here. I wont get into much detail, but by simply using a Grid, we can make our button look as we wish to make it look!

And also, with the use of triggers, all the cool looking effects when we hover over a Buttonm, or press it, can be achieved.

<--- Previous         Back to Index