WPF 如何修改button圆角(经典)
本人想设置Button为圆角,奈何搜索百度,找到的全是坑爹答案,现总结如下:
1. 需要添加button 的template.
2. 设置border的时候,必须要设置background, 否则会提示content 被多次使用。
1 2 3 4 5 6 7 8 9 10 | <Button Grid.Row= "3" Grid.Column= "2" Content= "取消" Margin= "30,40,200,40" > <Button.Template > <ControlTemplate TargetType= "{x:Type Button}" > <Border BorderBrush= "{TemplateBinding Control.BorderBrush}" BorderThickness= "1" CornerRadius= "7,7,7,7" > <Border.Background>#FFDDDDDD</Border.Background> <ContentPresenter Content= "{TemplateBinding ContentControl.Content}" HorizontalAlignment= "Center" VerticalAlignment= "Center" ></ContentPresenter> </Border> </ControlTemplate> </Button.Template> </Button> |
我们只需要在XAML中给他添加几行代码就可以做成圆角形状。
<Button x:Name="button" Content="按钮" FontSize="40" BorderThickness="0" HorizontalAlignment="Left" Margin="25,58,0,0" VerticalAlignment="Top" Width="472" Height="200" Foreground="White"> <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Border BorderThickness="1" BorderBrush="Black" CornerRadius="30" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> </ControlTemplate> </Button.Template> </Button>
属性解析:BorderThickness
:边框的大小BorderBrush
:边框的颜色CornerRadius
:圆角的大小Background
:背景颜色"{TemplateBinding Background}"
:这个就是使用上面<Button>
的Background属性值作为他的值<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
:文字垂直居中对齐
加个渐变色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <Button x:Name= "button" Content= "按钮" FontSize= "40" BorderThickness= "0" HorizontalAlignment= "Left" Margin= "25,58,0,0" VerticalAlignment= "Top" Width= "472" Height= "200" Foreground= "White" > <Button.Background> <LinearGradientBrush EndPoint= "1,1" StartPoint= "0,0" > <GradientStop Color= "#FFC564B8" Offset= "0" /> <GradientStop Color= "#FFF57A7A" Offset= "1" /> </LinearGradientBrush> </Button.Background> <Button.Template> <ControlTemplate TargetType= "{x:Type Button}" > <Border BorderThickness= "1" CornerRadius= "30" Background= "{TemplateBinding Background}" > <ContentPresenter VerticalAlignment= "Center" HorizontalAlignment= "Center" /> </Border> </ControlTemplate> </Button.Template> </Button> |
项目实例:
把样式和空间模板放到资源中,然后去引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <Window x:Class= "WpfApp18.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local= "clr-namespace:WpfApp18" mc:Ignorable= "d" Title= "MainWindow" Height= "450" Width= "800" > <Window.Resources > <ResourceDictionary > <Style x:Key= "dgButton" TargetType= "Button" > <Setter Property= "FontSize" Value= "40" /> <Setter Property= "Content" Value= "按钮" /> <Setter Property= "Foreground" Value= "White" /> <Setter Property= "Background" > <Setter.Value> <!--<RadialGradientBrush> <GradientStop Color= "#FFC564B8" Offset= "0" /> <GradientStop Color= "#FFF57A7A" Offset= "1" /> </RadialGradientBrush>--> <LinearGradientBrush EndPoint= "1,1" StartPoint= "0,0" > <GradientStop Color= "#FFC564B8" Offset= "0" /> <GradientStop Color= "#FFF57A7A" Offset= "1" /> </LinearGradientBrush> </Setter.Value> </Setter> </Style > <ControlTemplate x:Key= "buttonTemplate" TargetType= "Button" > <Border BorderThickness= "1" CornerRadius= "30" Background= "{TemplateBinding Background}" > <ContentPresenter VerticalAlignment= "Center" HorizontalAlignment= "Center" /> </Border> <!--<Grid > <Ellipse Name= "faceEllipse" Height= "50" Width= "100" Fill= "{TemplateBinding Button.Background}" /> <TextBlock Name= "txtBlock" /> </Grid >--> <ControlTemplate.Triggers > <Trigger Property= "Button.IsMouseOver" Value= "True" > <Setter Property= "Button.Background" Value= "blue" /> </Trigger > </ControlTemplate.Triggers > </ControlTemplate > </ResourceDictionary > </Window.Resources > <Grid> <Button Height= "200" HorizontalAlignment= "Center" Name= "button1" VerticalAlignment= "Center" Width= "400" Style = "{StaticResource dgButton}" Template= "{StaticResource buttonTemplate}" /> </Grid> </Window> |
鼠标放到按钮上以后按钮呈现蓝色
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)