Search code examples
wpfbuttonpathstylescontentpresenter

How do I completely replace the content of a WPF button with a graphic path?


I've deconstructed a standard WPF button using Blend and have managed to create a nicely styled button, but I cannot figure out how to make the path fill the interior of the button space (the button width and height). I am also not sure if I need to specify ContentPresenter or even if it is correct. I am after the text in the middle of the button (as normal) but with my graphic path behind it.

Can anyone give me feedback on how to accomplish this? The style is defined as;

    <ControlTemplate x:Key="CurvedButton" TargetType="{x:Type Button}">
        <Grid>
            <Path Fill="#ff951c1f" Data="F1 M 64,16 C 64,24 56,31 48,31 L 15,31 C 7,31 0,24 0,16 C 0,7 7,0 15,0 L 48,0 C 56,0 64,7 64,16 Z" />
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" />
        </Grid>
    </ControlTemplate>

The usage of this button is;

<StackPanel>
    <Button Template="{StaticResource CurvedButton}" FontFamily="MS Trebuchet" FontSize="40" Width="200" Height="120" Foreground="Black">XXXXXXXXXXX</Button>
</StackPanel>

When all is done, it should just look like a curvy red button.

Thanks in advance

Ryan


Solution

  • There are a couple things you can do to get the results you're looking for.

    Place the path in a viewbox and have it stretch to fill:

    <ControlTemplate x:Key="CurvedButton" TargetType="{x:Type Button}">
        <Grid>
            <Viewbox Stretch="Fill">
                <Path Fill="#ff951c1f" Data="F1 M 64,16 C 64,24 56,31 48,31 L 15,31 C 7,31 0,24 0,16 C 0,7 7,0 15,0 L 48,0 C 56,0 64,7 64,16 Z" />
            </Viewbox>
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" />
        </Grid>
    </ControlTemplate>
    

    Use a border instead of a path:

    <ControlTemplate x:Key="CurvedButton" TargetType="{x:Type Button}">
        <Grid>
            <Border CornerRadius="40" Background="#ff951c1f">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" />
            </Border>
        </Grid>
    </ControlTemplate>