0

In our project we have default style for button, which defines template for button and there is a Border with property CornerRadius="0":

<Style TargetType="Button" x:Key="TemplatedButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        CornerRadius="0"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        Cursor="{TemplateBinding Cursor}"
                        >
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                      RecognizesAccessKey="True"
                                      />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Button control has no property CornerRadius, so it's imposible to bind that property to TemplateBinding, but for some control we need to customize CornerRadius property:

<Button Style="{StaticResource TemplatedButton}" x:Name="btn1" Content="Button with radius" /*HERE WE NEED TO SET CUSTOM CornerRadius*/ />

Is it possible to somehow change that Corner radius? Maybe in style that inherits it or by adding resources to btn1?

I saw that it possible to customize default properties by setting resource with same key to different value, but don't know how to implement that. Will appreciate if you provide any tutorials or documentation on how to implement that.

Epsil0neR
  • 1,676
  • 16
  • 24

1 Answers1

0

You need to create a new class that inherits from Button, and add the required property

public class ExtendedButton: Button
{
    public CornerRadius CornerRadius
    {
        get { return (CornerRadius)GetValue(CornerRadiusProperty); }
        set { SetValue(CornerRadiusProperty, value); }
    }

    public static readonly DependencyProperty CornerRadiusProperty =
        DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ExtendedButton), new PropertyMetadata(default(CornerRadius)));
}

which you can then use in place of a standard button in your style / view.

Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • Yeah, I know that way of coding, for this case it can work, but always create nesting control is not a solution, it is pretty heavy. Thanks for your solution, but I think I found waht I was looking: using DynamicResource and in Resources of control (or resources of nested style) declare ` – Epsil0neR Sep 26 '18 at 09:38