Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a style in WPF

I've made the default button to look like this:

<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        SnapsToDevicePixels="true"
                        CornerRadius="0">
                    <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I want for the Login button to be extended from the default button only to change the "CornerRadius" property witch is in border so i could make it round, without making another style/template or using C# code. I want to do it like this or something like it:

 <Style x:Key="loginButton" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="CornerdRadius" Value="10"/>
 </Style>
like image 506
Toody Avatar asked Nov 18 '25 01:11

Toody


1 Answers

Give your style a name, then make a key-less style that’s just based on it.

<Style x:Key="BaseButtonStyle" TargetType="{x:Type Button}">
     <Setter ... />
</Style>

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}"/>

Then make your second style based on your base style, for your connect button.

<Style x:Key="LoginButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}">
    <Setter ... />
</Style>

edit :

There is no "CornerRadius" propery in the WPF button. You would have to make a subclass that inherits Button and adds a CornerRadius dependency property. Only then you can inherit your style like you want, and only change one setter.

If you don't want to subclass Button, you'll have to do a second style with another template and rewrite all the triggers, unfortunately.

Another option is to use an existing property like BorderThickness, Padding or Margin, and use that with a TemplateBinding in your ControlTemplate. But it's kind of a "hack" because the name of the property does not equal its function.

edit 2 :

As suggested by ASh, you may also use attached dependency properties to solve your problem. https://stackoverflow.com/a/40663219/1506454

like image 123
FatalJamòn Avatar answered Nov 20 '25 17:11

FatalJamòn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!