WPF 如何修改button圆角(经典)

开发 前端
本人想设置Button为圆角,奈何搜索百度,找到的全是坑爹答案,现总结如下:1. 需要添加button 的template.2. 设置border的时候,必须要设置background, 否则会提示content 被多次使用。

 [[381159]]

本人想设置Button为圆角,奈何搜索百度,找到的全是坑爹答案,现总结如下:

1. 需要添加button 的template.

2. 设置border的时候,必须要设置background, 否则会提示content 被多次使用。

  1. <Button Grid.Row="3" Grid.Column="2" Content="取消" Margin="30,40,200,40" > 
  2.                 <Button.Template > 
  3.                     <ControlTemplate TargetType="{x:Type Button}" > 
  4.                         <Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="1" CornerRadius="7,7,7,7"
  5.                             <Border.Background>#FFDDDDDD</Border.Background> 
  6.                             <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" ></ContentPresenter> 
  7.                         </Border> 
  8.                     </ControlTemplate> 
  9.                 </Button.Template> 
  10.             </Button> 

我们只需要在XAML中给他添加几行代码就可以做成圆角形状。

  1. <Button x:Name="button" Content="按钮" FontSize="40" BorderThickness="0" HorizontalAlignment="Left" Margin="25,58,0,0" VerticalAlignment="Top" Width="472" Height="200" Foreground="White"
  2.       <Button.Template> 
  3.            <ControlTemplate TargetType="{x:Type Button}"
  4.                <Border BorderThickness="1" BorderBrush="Black" CornerRadius="30" Background="{TemplateBinding Background}"
  5.                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> 
  6.                </Border> 
  7.            </ControlTemplate> 
  8.        </Button.Template> 
  9. </Button> 

属性解析:

BorderThickness:边框的大小

BorderBrush:边框的颜色

CornerRadius:圆角的大小

Background:背景颜色"{TemplateBinding Background}":这个就是使用上面<Button>的Background属性值作为他的值

<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>:文字垂直居中对齐

加个渐变色

  1. <Button x:Name="button" Content="按钮" FontSize="40" BorderThickness="0" HorizontalAlignment="Left" Margin="25,58,0,0" VerticalAlignment="Top" Width="472" Height="200" Foreground="White"
  2.             <Button.Background> 
  3.                 <LinearGradientBrush EndPoint="1,1" StartPoint="0,0"
  4.                     <GradientStop Color="#FFC564B8" Offset="0"/> 
  5.                     <GradientStop Color="#FFF57A7A" Offset="1"/> 
  6.                 </LinearGradientBrush> 
  7.             </Button.Background> 
  8.             <Button.Template> 
  9.                 <ControlTemplate TargetType="{x:Type Button}"
  10.                     <Border BorderThickness="1" CornerRadius="30" Background="{TemplateBinding Background}"
  11.                         <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> 
  12.                     </Border> 
  13.                 </ControlTemplate> 
  14.             </Button.Template> 
  15.         </Button> 

如图:

项目实例:

把样式和空间模板放到资源中,然后去引用

  1. <Window x:Class="WpfApp18.MainWindow" 
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.         xmlns:local="clr-namespace:WpfApp18" 
  7.         mc:Ignorable="d" 
  8.         Title="MainWindow" Height="450" Width="800"
  9.     <Window.Resources > 
  10.         <ResourceDictionary > 
  11.             <Style x:Key="dgButton" TargetType="Button" > 
  12.                 <Setter Property="FontSize" Value="40"/> 
  13.                 <Setter Property="Content" Value="按钮"/> 
  14.                 <Setter Property="Foreground" Value="White"/> 
  15.                 <Setter Property="Background"
  16.                     <Setter.Value> 
  17.                         <!--<RadialGradientBrush> 
  18.                         <GradientStop Color="#FFC564B8" Offset="0"/> 
  19.                         <GradientStop Color="#FFF57A7A" Offset="1"/> 
  20.                     </RadialGradientBrush>--> 
  21.                         <LinearGradientBrush EndPoint="1,1" StartPoint="0,0"
  22.                             <GradientStop Color="#FFC564B8" Offset="0"/> 
  23.                             <GradientStop Color="#FFF57A7A" Offset="1"/> 
  24.                         </LinearGradientBrush> 
  25.                     </Setter.Value> 
  26.                 </Setter> 
  27.             </Style > 
  28.             <ControlTemplate x:Key="buttonTemplate" TargetType="Button" > 
  29.                 <Border BorderThickness="1" CornerRadius="30" Background="{TemplateBinding Background}"
  30.                     <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> 
  31.                 </Border> 
  32.                 <!--<Grid > 
  33.                     <Ellipse Name="faceEllipse" Height="50" Width="100" Fill="{TemplateBinding Button.Background}"/> 
  34.                     <TextBlock Name="txtBlock"  /> 
  35.                 </Grid >--> 
  36.                 <ControlTemplate.Triggers > 
  37.                     <Trigger Property="Button.IsMouseOver" Value="True"
  38.                         <Setter Property="Button.Background" Value="blue"/> 
  39.                     </Trigger > 
  40.                 </ControlTemplate.Triggers > 
  41.             </ControlTemplate > 
  42.         </ResourceDictionary > 
  43.     </Window.Resources > 
  44.     <Grid> 
  45.         <Button Height="200" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center" Width="400" Style ="{StaticResource dgButton}" Template="{StaticResource  buttonTemplate}"/> 
  46.     </Grid> 
  47. </Window> 

本文转载自微信公众号「CSharp编程大全」,可以通过以下二维码关注。转载本文请联系CSharp编程大全公众号。


【编辑推荐】

 

责任编辑:武晓燕 来源: CSharp编程大全
相关推荐

2009-09-11 10:25:35

C# button样式

2009-09-10 17:48:05

C# button

2009-12-24 15:59:46

WPF图像格式

2010-03-30 10:12:41

Nginx php.i

2009-12-24 16:57:53

WPF密码

2009-04-27 09:41:01

C#WPFTemplate

2022-07-11 21:52:29

CSS滤镜构建圆角

2014-09-24 11:42:46

AndroidButton

2018-03-05 21:39:38

AWSIoT Button网络

2009-09-11 09:17:00

C# Button

2012-02-08 10:16:43

WPF

2023-01-16 08:11:49

Edge浏览器

2013-07-24 18:14:36

Android开发学习Android UIButton

2011-07-21 16:10:11

button按钮jQuery Mobi

2022-01-06 07:18:18

Kafka选举Leader

2010-01-26 10:02:51

Android But

2013-07-29 11:34:46

iOS开发iOS开发学习设置UITextVie

2010-08-26 15:59:38

DIV圆角

2009-12-28 10:47:58

WPF绘图

2009-12-25 17:39:01

WPF验证
点赞
收藏

51CTO技术栈公众号