定制异型按钮在WPF [英] Custom Shaped Button in WPF

查看:672
本文介绍了定制异型按钮在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个按钮,显示在图片更是把形状的要求:

I have a requirement of creating a button which takes the shapes as displayed in the picture:

谁能帮帮我吗? !
在此先感谢

Can anyone please help me? Thanks in advance!

推荐答案

您可以使用的ControlTemplate来实现这一目标:

You could use a ControlTemplate to achieve that:

<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>



比你把它应用到按钮:

Than you apply it to the button:

<Button Style="{StaticResource ButtonStyle}"/>

如果你需要一些参考绘制路径检查的this MSDN链接。

If you need some references to draw the "Path" check this MSDN link.

更新

要告诉你应该使用ContentPresenter,像这样的内容:

To show the content you should use a ContentPresenter, something like this:

<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>

在按钮:

<Button Style="{StaticResource ButtonStyle}" Foreground="White">
        test
    </Button>



在这里输入的形象描述

这篇关于定制异型按钮在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆