首页 > 编程语言 >C# WPF UI开发自定义圆形按钮样式

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

时间:2023-02-14 08:55:14浏览次数:42  
标签:自定义 方式 C# 样式 圆形 UI 画圆 按钮 CornerRadius

圆形按钮有很多种实现方式,例如第一种方式在样式中用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}">
                                   //此处修改CornerRadius="5" 可转换为圆角矩形
                                   <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>

 

标签:自定义,方式,C#,样式,圆形,UI,画圆,按钮,CornerRadius
From: https://www.cnblogs.com/guangzhiruijie/p/17118513.html

相关文章

  • springboot自动配置原理以及spring.factories文件的作用详解
    一、springboot自动配置原理先说说我们自己的应用程序中Bean加入容器的办法:packagecom.ynunicom.dc.dingdingcontractapp;importcom.alibaba.druid.spring.boot.au......
  • MySQL 8.0 修改root远程登录【ERROR 1410 (42000): You are not allowed to create a
    MySQL8.0的数据库root用户默认无法远程登录,需要修改root的远程授权,如下:mysql>grantallprivilegeson*.*to'root'@'%';ERROR1410(42000):Youarenotallowed......
  • docker compose services name冲突
    这两天升级了家庭服务器,内存32G了,所以开始把一些自己会用到的服务,部署在自己的服务器上。折腾的时候,发生了一些小状况,特记录如下: 我是使用IPV6来进行服务的,docker的ip......
  • ClickHouse 查询优化详细介绍
     你想要的ClickHouse优化,都在这里。ClickHouse是OLAP(Onlineanalyticalprocessing)数据库,以速度见长[1]。ClickHouse为什么能这么快?有两点原因[2]:架构优越......
  • rocketMq启动时报内存溢出
    原因:rocketMq默认的内存设置过大,所以启动报错 处理:修改内存设置,调小即可修改runserver.cmd文件 ......
  • 周末折腾了两天,踩了无数个坑,终于把win7装成了centos7
    上周五的时候,突发奇想,想把自己的ThinkpadE430C的操作系统装成linux。熟悉电脑的都知道ThinkpadE430C很古老了,现在算来从2012年买来,到现在已经经历了10个年头了。原厂是4......
  • CF446C DZY Loves Fibonacci Numbers 题解和加强
    简要题意https://www.luogu.com.cn/problem/CF446C给定一个长度为\(n\)的序列\(A\),要求支持两种操作:1给定区间\((l,r)\)对这个区间内的每个数,依次加斐波那契数列......
  • 用ChatGPT来了解ChatGPT
    用ChatGPT来了解ChatGPT之前学习一个新技术,想着要搞清楚这6个问题(来自陈皓介绍的学习方法):1.这个技术出现的背景,初衷,要达到什么样的目标或是要解决什么样的问题......
  • C++开发原生WIN32程序
    VS2019文件-新建-项目-Windows桌面向导(C++)-桌面应用程序 空项目项目属性-高级-字符集未设置程序内所有字符串用TEXT宏包裹1#include<windows.h>23LONGWI......
  • 奋斗百天我要xueC--03
    0x00来啦来啦0x01函数函数不能嵌套,故定义的函数要放在main函数之上(先定义后调用)intsum(intm,intn){xxxxreturnxx}函数声明,用在函数定义前,就被调用的情况,保证程......