Override general button style

We have a common style in default.xaml designed for all buttons in our application. Is there a way to override this style and create a new button based on the default button?

+4
source share
3 answers

if you want to inherit any existing style, use BasedOn

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton"> <Setter Property="Foreground" Value="Red" /> </Style> 

this inherits the default style and the new foreground property will apply to the new style

hope this helps.

+10
source

You can change the button template like this

 <Style x:Key="{x:Type Button}" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Padding="10,5,10,5" BorderBrush="Green" BorderThickness="1" CornerRadius="5" Background="#EFEFEF"> <TextBlock Text="{TemplateBinding Content}" /> </Border> </ControlTemplate> </Style> 
+1
source

The Style class has a BasedOn property and the constructor takes a Style parameter, which determines the style on which it will be based.

 Style boldStyle = new Style(typeof(Button), generalButtonStyle); 
0
source

Source: https://habr.com/ru/post/1313744/


All Articles