1

I created a WPF Class Library so that it would contain base styles that will be used in WPF application. In that class, I wanted to create like base styles for like buttons, expanders, etc. where say if I want to re-use it for different WPF project I can just call those base styles.

I am just having issue with Button ResourceDictionary, I can't seem to be able to make the button corners to be rounded. Also, I am still learning to create ResourceDictionary, so, I would like to also know how can I link like attributes when I assign the style (for instance, Button) and say like Border colour to be red instead assigning them in the ResourceDictionary as constants. I want to have the ability to customize the colours/cornerradius if I want to re-use styles in different projects.

I have created the following ResourceDictionary for Round Button,

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Define the ButtonStyle style -->
    <Style x:Key="CustomButtonStyle" TargetType="Button">
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Rectangle x:Name="background" Fill="{TemplateBinding Background}"/>
                        <Border x:Name="border" CornerRadius="{TemplateBinding Border.CornerRadius}" Background="{TemplateBinding Border.Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <!--<Setter TargetName="background" Property="Fill" Value="{TemplateBinding Background}" />-->
                            <Setter TargetName="background" Property="Opacity" Value="0.5"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <!--<Setter TargetName="background" Property="Fill" Value="{TemplateBinding Background}" />-->
                            <Setter TargetName="background" Property="Opacity" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <!--<Setter TargetName="background" Property="Fill" Value="{TemplateBinding Background}" />-->
                            <Setter Property="BorderThickness" Value="0" TargetName="border"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Cursor" Value="Hand"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

As mentioned in the question earlier, I was not able to assign a value for CornerRadius. Can someone point out if I made a mistake and/or I am missing something?

1
  • CornerRadius="{TemplateBinding Border.CornerRadius}" makes no sense, because Button does not have a CornerRadius (or Border.CornerRadius) property. You would either set a constant value or perhaps use a DynamicResource expression. Or you derive a custom class from Button and add a CornerRadius property. Commented Jul 16 at 10:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.