【WPF】自定义形状的按钮Button

需求:做一个如下图所示的多边形按钮。

<!-- 特殊形状的按钮 -->
<Grid>
    <Polygon Points="0,0 140,0 190,42 140,84 0,84" MouseDown="YourCommand"
             Stroke="#FFE00E73" StrokeThickness="1" VerticalAlignment="Center" Margin="10">
        <Polygon.Fill>
            <SolidColorBrush Color="White"/>
        </Polygon.Fill>
    </Polygon>
    <Label Content="开始设计" MouseDown="YourCommand" VerticalAlignment="Center" HorizontalAlignment="Center" 
           FontSize="24" Foreground="#FFE00E73" Margin="-30,0,0,0"/>
</Grid>

Points点从左上角(0, 0)点开始,顺时针绘制,最后回到原点完成封闭的图形。

由于多边形Polygon没有内部显示文字的属性,用一个Grid将它和Label包裹到一起。

Label也要加上与Polygon相同的MouseDown点击事件,否则点击到Label上将没有反应(事件被Label拦截,Polygon无法接收到)。

原文地址:https://www.cnblogs.com/guxin/p/wpf-custom-shaped-button-by-polygon.html