Every Style Element requires a Key, so that we can refer it anywhere using their Key. It supports only named types. It requires TargetType, which is the type of an element on which you r applying a style.
There are three ways to use Styles:
1. You can apply styles to particular page.
2. You can apply styles to particular Control.
3. You can apply styles globally.
Let us see all the steps one by one.
1. Applying Styles to a particular page:
Defining:
First we need to define the style in UserControl.Resource section like,
1.gif
A Setter element is a definition of one property to set on the target object. A Setter has a two Property one is Property naming the property to set on the target and another one is Value property defining the value to set on the property.
Here is your Page.xaml code:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
Applying:
Then you can apply the style which you have created using the Style property of a Button as shown in the below code.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
2. Applying Styles to a particular Control:
To use Style for a particular Control, place the style directly in the control. Here, no need to mention the x:Key attribute.
For Example,
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
3. Applying Styles Globally:
To use the Styles globally, i.e., you can use the Styles in any xaml pages throughout your application, for that you need to define the Style in Application.Resource in App.xaml file.
x:Class="styling.App">
Here is your xaml file.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
Solution by:
No comments:
Post a Comment