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

1. 需要添加button 的template.

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

            <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"/>:文字垂直居中对齐

加个渐变色

<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>

  

项目实例:

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

<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>

  鼠标放到按钮上以后按钮呈现蓝色

WPF 如何修改button圆角(经典)的更多相关文章

  1. WPF button 圆角制作

    将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="{x:Type Button}"&g ...

  2. WPF 中的 button style 的修改

    <Style x:Key="ButtonStyleTransBack" TargetType="Button"> <Setter Proper ...

  3. C# Wpf异步修改UI,多线程修改UI(二)

    1.使用定时器异步修改 这是相对比较简单的方法 在Wpf中定时器使用DiapatcherTimer,不使用Timer原因: 在一个应用程序中,Timer会重复生成time事件,而DispatcherT ...

  4. [置顶] WPF数据修改demo

    今天晚上研究了下wpf,现在把代码贴出来供大家学习参考 sql语句: create table userinfos ( ContactID  int primary key identity(1,1) ...

  5. Android Demo---如何敲出圆角的Button+圆角头像

    经常玩儿App的小伙伴都知道,APP上面有很多按钮都是圆角的,圆形给人感觉饱满,富有张力,不知道设计圆角按钮的小伙伴是不是和小编有着相同的想法`(*∩_∩*)′,听小编公司开发IOS的小伙伴说,他们里 ...

  6. 【转】【WPF】WPF中的Button的MouseDown事件不触发问题

    按照WPF的帮助说明,某些控件的路由事件被内部处理了,已经被标记为Handled,自行定义的事件处理代码便不再起作用了,有时候会很郁闷! 不过WPF提供了必要的方法. 1)使用相应的Preview事件 ...

  7. 修改button的可点击区域

    需求:在cocos2dx引擎中,button的点击区域和button图片的大小是一样的,但是我需要修改可点击区域的大小和位置,需要修改引擎源码: button提供的接口中并没有和touch相关,but ...

  8. 【C#/WPF】修改图像的DPI、Resolution

    问题: WPF中默认使用的图像的DPI是96.如果我们使用的图素的DPI不是96时(比如是72),那么WPF会把图片的DPI自动改为96,导致图像加载出来的实际大小Width和Height会比想要的大 ...

  9. 【WPF】修改ComboBox样式

    修改WPF默认的ComboBox控件样式 如下图所示: 修改代码如下: <UserControl.Resources> <Style TargetType="ToggleB ...

随机推荐

  1. Spark练习之wordcount,基于排序机制的wordcount

    Spark练习之wordcount 一.原理及其剖析 二.pom.xml 三.使用Java进行spark的wordcount练习 四.使用scala进行spark的wordcount练习 五.基于排序 ...

  2. 函数式编程(hashlib模块)

      hashlib模块 一.hashlib模块 hashlib模块,主要用于加密相关的操作,在python3的版本里,代替了md5和sha模块,主要提供 SHA1, SHA224, SHA256, S ...

  3. python中如何添加模块导入路径?

    python中自定义模块导入路径的方式主要有以下3种: (1)使用sys.path.append() 随着程序执行,会动态地添加模块导入的路径,但是程序执行结束后就会立即失效(临时性的) import ...

  4. DEDECMS:DEDE整合(UEditor)百度编辑器以后,栏目内容、单页无法保存内容的bug处理方法

    已经整合过百度编辑器的站长们或许会发现,在编辑单页文档和栏目内容的时候,百度编辑器不能够保存新增或已修改数据,经过排查后发现问题出现在catalog_edit.htm.catalog_add.htm这 ...

  5. Inceptor常用SQL

    1.创建数据库 建一个数据库exchange_platform. DROP DATABASE IF EXISTS exchange_platform CASCADE; CREATE DATABASE ...

  6. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)【ABCDF】

    比赛链接:https://codeforces.com/contest/1443 A. Kids Seating 题意 构造一个大小为 \(n\) 的数组使得任意两个数既不互质也不相互整除,要求所有数 ...

  7. HDU 3488-Tour KM

    为什么可以这样拆点在 这道题 都已经证明过 代码: 1 //题目上面说了"The only exception is that the first and the last city sho ...

  8. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)

    题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...

  9. CF1462-C. Unique Number

    题意: 给出一个数字x,让你找出一个由1到9这九个数字组成的数字,这个数字的每一位加起来等于x,并且1到9每个数字只能出现一次.若能找到这样的数字,输出这其中最小的一个,否则输出-1. 思路: 利用二 ...

  10. PHP 弱类型 && CODE/COMMADN injection

    CODE/COMMAND INJECTION CODE INJECTION https://www.freebuf.com/sectool/168653.html EXAMPLE1 <?php ...