本人想设置Button为圆角,奈何搜索百度,找到的全是坑爹答案,现总结如下:
需要添加button 的template.
设置border的时候,必须要设置background, 否则会提示content 被多次使用。
-
-
"{x:Type Button}" > -
"{TemplateBinding Control.BorderBrush}" BorderThickness="1" CornerRadius="7,7,7,7"> -
#FFDDDDDD -
"{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" > -
-
-
-
我们只需要在XAML中给他添加几行代码就可以做成圆角形状。
-
-
"{x:Type Button}" > -
"1" BorderBrush="Black" CornerRadius="30" Background="{TemplateBinding Background}"> -
"Center" HorizontalAlignment="Center"/> -
-
-
-
属性解析:
BorderThickness:边框的大小
BorderBrush:边框的颜色
CornerRadius:圆角的大小
Background:背景颜色"{TemplateBinding Background}":这个就是使用上面
加个渐变色
-
-
"1,1" StartPoint="0,0"> -
"#FFC564B8" Offset="0"/> -
"#FFF57A7A" Offset="1"/> -
-
-
-
"{x:Type Button}" > -
"1" CornerRadius="30" Background="{TemplateBinding Background}"> -
"Center" HorizontalAlignment="Center"/> -
-
-
-
如图:
项目实例:
把样式和空间模板放到资源中,然后去引用
"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">
-
-
-
-
Key ="buttonTemplate" TargetType="Button" > -
"1" CornerRadius="30" Background="{TemplateBinding Background}"> -
"Center" HorizontalAlignment="Center"/> -
- --
-
Name ="faceEllipse" Height="50" Width="100" Fill="{TemplateBinding Button.Background}"/> -
Name ="txtBlock" /> - -->
-
- <Trigger Property="Button.IsMouseOver" Value="True">
-
"Button.Background" Value="blue"/> - Trigger >
-
-
-
-
-
- HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center" Width="400" Style ="{StaticResource dgButton}" Template="{StaticResource buttonTemplate}"/>
-
-
本文转载自微信公众号「CSharp编程大全」,可以通过以下二维码关注。转载本文请联系CSharp编程大全公众号。
【编辑推荐】