WPF UI开发自定义圆形按钮样式

92 篇文章 18 订阅
92 篇文章 23 订阅

在程序开发中,经常用到Button,而且经常用到各种形状Button,这次教程就讲个比较简单圆形按钮。

圆形按钮有很多种实现方式,例如第一种方式在样式中用Ellipse画一个圆形,宽度和高度一致。第二种方式用Border设置Width,Height,CornerRadius一致也可以画圆。第三种方式用Path用矢量图画圆。

三种方式的按钮我都已经实现,看一下效果:

以下第一种方式在样式中用Ellipse画一个圆形实现方式。

<Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="#3099C5"/>
                    <Setter Property="IsHitTestVisible" Value="True"/>
                    <Setter Property="Width" Value="64"/>
                    <Setter Property="Height" Value="64"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ButtonBase}">
                                <Grid SnapsToDevicePixels="True">
                                    <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" 
                                             Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"  />
                                    <ContentControl Content="{TemplateBinding Content}" Focusable="False" 
                                             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

以下第二种方式用Border设置Width,Height,CornerRadius一致画圆。

 <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="#3099C5"/>
                    <Setter Property="IsHitTestVisible" Value="True"/>
                    <Setter Property="Width" Value="64"/>
                    <Setter Property="Height" Value="64"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ButtonBase}">
                                <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
                                        CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Height="{TemplateBinding Height}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                    <ContentControl Content="{TemplateBinding Content}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>                                
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

第三种方式用Path用矢量图画圆。

还有其它方式就不举例了,大家也可以用其它方式实现。

<Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="#3099C5"/>
                    <Setter Property="IsHitTestVisible" Value="True"/>
                    <Setter Property="Width" Value="64"/>
                    <Setter Property="Height" Value="64"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ButtonBase}">
                                <Grid>
                                    <Viewbox Stretch="Uniform">
                                        <Grid>
                                            <Path Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" Data="M510.537016 1014.078844c-67.964659 0-133.907178-13.315997-195.994961-39.576884-59.959897-25.361049-113.805489-61.663802-160.04137-107.897688-46.234883-46.23588-82.536638-100.081472-107.897687-160.041369C20.342111 644.474122 7.026114 578.532601 7.026114 510.567942s13.315997-133.907178 39.576884-195.994962c25.361049-59.959897 61.663802-113.805489 107.897687-160.041369 46.23588-46.234883 100.080475-82.536638 160.04137-107.897687C376.629838 20.373037 442.572357 7.05704 510.537016 7.05704S644.444194 20.373037 706.531977 46.633924c59.959897 25.361049 113.805489 61.663802 160.04137 107.897687 46.234883 46.23588 82.536638 100.080475 107.897687 160.041369 26.260887 62.087783 39.576884 128.030302 39.576884 195.994962s-13.315997 133.907178-39.576884 195.994961c-25.361049 59.959897-61.663802 113.805489-107.897687 160.041369-46.23588 46.234883-100.080475 82.536638-160.04137 107.897688-62.088781 26.260887-128.030302 39.576884-195.994961 39.576884z m0-967.117707c-255.633631 0-463.606804 207.973174-463.606804 463.606805s207.973174 463.606804 463.606804 463.606804 463.606804-207.973174 463.606804-463.606804S766.170647 46.961137 510.537016 46.961137z"/>
                                            <Path Fill="{TemplateBinding Background}"  Data="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z"/>
                                        </Grid>
                                    </Viewbox>
                                    <ContentControl Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Grid>                                                                                          
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

 推荐一款WPF MVVM框架开源项目:Newbeecoder.UI

Newbeecoder.UI开源项目

Demo下载:

Newbeecoder.UI开源项目icon-default.png?t=M3K6https://share.weiyun.com/py6W1dcK

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 WPF 中的 Button 类并实现 ICommandSource 接口来创建一个自定义控件,该控件可以带有参数命令。首先,创建一个类,继承 Button 类并实现 ICommandSource 接口,如下所示: ```csharp public class CommandButton : Button, ICommandSource { public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( "CommandParameter", typeof(object), typeof(CommandButton), new PropertyMetadata(default(object))); public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof(ICommand), typeof(CommandButton), new PropertyMetadata(default(ICommand))); protected override void OnClick() { if (Command != null) { Command.Execute(CommandParameter); } base.OnClick(); } } ``` 在这个自定义控件中,我们定义了一个名为 CommandButton 的类,其中包含一个名为 CommandParameter 的依赖属性和一个名为 Command 的依赖属性。我们还重写了 OnClick 方法,在单击按钮时执行与命令相关的操作。 现在,您可以在 XAML 中使用这个自定义控件,并为 Command 和 CommandParameter 属性设置值,如下所示: ```xml <local:CommandButton Content="Click Me!" Command="{Binding MyCommand}" CommandParameter="parameter" /> ``` 其中,local 是指您的自定义控件所在的命名空间。MyCommand 是您的 ViewModel 中实现的命令,parameter 是该命令的参数。当用户单击按钮时,命令将被执行,并且参数将被传递给命令的 Execute 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值