Inheriting a style from another style

Just like class inheritance, you can also inherit a style from another base style by using the property BasedOn. This reduces duplicate style setters and gives you a clean, easy-to-maintain code.

Let us create one base Style for button control having only two setters to change its background and foreground color. As we are going to inherit this style, let's give it the name ButtonBaseStyle by defining the x:Key attribute:

  <Style x:Key="ButtonBaseStyle" TargetType="Button"> 
      <Setter Property="Background" Value="Orange"/> 
      <Setter Property="Foreground" Value="White"/> 
  </Style> 

Create another style with two more setters to define the button's height and width properties. Now inherit the first style by setting the BasedOn property of the second style referencing the name of the base as follows:

  <Style TargetType="Button"  
         BasedOn="{StaticResource ButtonBaseStyle}"> 
      <Setter Property="Height" Value="50"/> 
      <Setter Property="Width" Value="100"/> 
  </Style> 

Now, due to its implicit behavior, all the button controls in the page will get the second style applied. As it is referencing the first style (that is, ButtonBaseStyle) as a base reference, the style properties defined there will get inherited and applied to the buttons automatically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.162