WPF中的自定义形状按钮

Pra*_*ana 7 c# wpf custom-controls wpf-controls

我需要创建一个按钮,它采用图片中显示的形状:

在此输入图像描述

谁能帮帮我吗?提前致谢!

cbc*_*col 22

您可以使用ControlTemplate来实现:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Path Fill="{TemplateBinding Background}"
                            Data="M 0,0 A 100,100 90 0 0 100,100 L 100,100 100,0" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

比你把它应用到按钮:

<Button Style="{StaticResource ButtonStyle}"/>
Run Code Online (Sandbox Code Playgroud)

如果您需要一些引用来绘制"路径",请检查此 MSDN链接.

更新

要显示内容,您应该使用ContentPresenter,如下所示:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Path Fill="{TemplateBinding Background}"
                            Data="M 0,0 A 100,100 90 0 0 100,100 L 100,100 100,0" />
                        <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

在按钮中:

<Button Style="{StaticResource ButtonStyle}" Foreground="White">
        test
    </Button>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述