Author : Admin
Last Modified : 27-Apr-2020
Complexity : Beginner
How to inherit the style from default style in WPF
WPF allows specifying the new style base on the current style. In this way, you can define a base style with some common set of properties and inherit it to create a new style for some specific control.
BaseOn Property of Style is used for it
Let's understand it with an example.
Suppose we have defined a style for TextBlock
<Style x:Key="tbBaseStyle" TargetType="TextBlock"> <Setter Property="FontSize" Value="11" /> <Setter Property="Background" Value="Green" /> <Setter Property="Foreground" Value="White" /> </Style>
So all TextBlocks, using the style "tbBaseStyle" will have font size 11, background Green and Foreground White. But for some TextBlocks, we require a style also having FontWeight Bold with Red Background. We can use BaseOn to extend the Current tbBaseStyle style to create a new style
<Style x:Key="tbnewBaseStyle" BasedOn="{StaticResource tbBaseStyle}" TargetType="TextBlock"> <Setter Property="Background" Value="Red" /> <Setter Property="FontWeight" Value="Bold" /> </Style>
In this way, we can drive a new style from a current style and also If there is any change in the base style it will also reflect in drive style controls.