当前位置:主页 > 查看内容

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

发布时间:2021-06-08 00:00| 位朋友查看

简介:本人想设置Button为圆角,奈何搜索百度,找到的全是坑爹答案,现总结如下: 1. 需要添加button 的template. 2. 设置border的时候,必须要设置background, 否则会提示content 被多次使用。 ButtonGrid.Row= 3 Grid. Column = 2 Content= 取消 Margin= 30,40,2……

 

本人想设置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编程大全公众号


【编辑推荐】

  1. 自定义监控模板来监控nginx edispvuv服务
  2. Kubernetes集群高可用安装通用模板(v1.20.x)
  3. 消灭JavaScript怪兽第三季(1-4):块作用域/解构赋值/模板字符串
  4. 可能学了假的编程?C++新标准难点解析之可变模板参数
  5. 微软累积更新导致Visual Studio、WPF等应用崩溃 修复补丁即将发布

本文转载自网络,原文链接:https://mp.weixin.qq.com/s/wos9rbaFG59CfXg2LYSntg
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐