WPF 如何修改button圆角(经典)
本人想设置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圆角(经典)的更多相关文章
- WPF button 圆角制作
将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="{x:Type Button}"&g ...
- WPF 中的 button style 的修改
<Style x:Key="ButtonStyleTransBack" TargetType="Button"> <Setter Proper ...
- C# Wpf异步修改UI,多线程修改UI(二)
1.使用定时器异步修改 这是相对比较简单的方法 在Wpf中定时器使用DiapatcherTimer,不使用Timer原因: 在一个应用程序中,Timer会重复生成time事件,而DispatcherT ...
- [置顶] WPF数据修改demo
今天晚上研究了下wpf,现在把代码贴出来供大家学习参考 sql语句: create table userinfos ( ContactID int primary key identity(1,1) ...
- Android Demo---如何敲出圆角的Button+圆角头像
经常玩儿App的小伙伴都知道,APP上面有很多按钮都是圆角的,圆形给人感觉饱满,富有张力,不知道设计圆角按钮的小伙伴是不是和小编有着相同的想法`(*∩_∩*)′,听小编公司开发IOS的小伙伴说,他们里 ...
- 【转】【WPF】WPF中的Button的MouseDown事件不触发问题
按照WPF的帮助说明,某些控件的路由事件被内部处理了,已经被标记为Handled,自行定义的事件处理代码便不再起作用了,有时候会很郁闷! 不过WPF提供了必要的方法. 1)使用相应的Preview事件 ...
- 修改button的可点击区域
需求:在cocos2dx引擎中,button的点击区域和button图片的大小是一样的,但是我需要修改可点击区域的大小和位置,需要修改引擎源码: button提供的接口中并没有和touch相关,but ...
- 【C#/WPF】修改图像的DPI、Resolution
问题: WPF中默认使用的图像的DPI是96.如果我们使用的图素的DPI不是96时(比如是72),那么WPF会把图片的DPI自动改为96,导致图像加载出来的实际大小Width和Height会比想要的大 ...
- 【WPF】修改ComboBox样式
修改WPF默认的ComboBox控件样式 如下图所示: 修改代码如下: <UserControl.Resources> <Style TargetType="ToggleB ...
随机推荐
- Spark Straming,Spark Streaming与Storm的对比分析
Spark Straming,Spark Streaming与Storm的对比分析 一.大数据实时计算介绍 二.大数据实时计算原理 三.Spark Streaming简介 3.1 SparkStrea ...
- arp病毒系列——攻击类型
到目前为止,我所见闻的arp病毒攻击导致局域网几乎瘫痪的事例已经不下3次了,而且非常巧的是:每次都是将近学校考试.大批同学新下四楼更新IP-Mac的时候出现!严重的时候你根本就ping不通网关 ...
- 每个开发人员都应该知道的WebSockets知识
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/deep-dive-into-websockets- ...
- 牛客15334 Easygoing Single Tune Circulation(后缀自动机+字典树)
传送门:Easygoing Single Tune Circulation 题意 给定n个字符串 s[i],再给出m个查询的字符串 t[i],问 t[i] 是否为某个 s[i] 循环无限次的子串. 题 ...
- AtCoder Beginner Contest 164
比赛链接:https://atcoder.jp/contests/abc164 A - Sheep and Wolves #include <bits/stdc++.h> using na ...
- B. Queue
During the lunch break all n Berland State University students lined up in the food court. However, ...
- 开源RPA软件试用
优点 缺点 其它 Robot Framework 可视化界面 运行环境搭建复杂,依赖较多 操作复杂 倾向于自动化测试 TagUI 浏览器支持好 官方文档详细 命令行操作 非浏览器程序支持一般 ...
- .NET并发编程-函数闭包
本系列学习在.NET中的并发并行编程模式,实战技巧 内容目录 函数式编程闭包的应用记忆化函数缓存 函数式编程 一个函数输出当做另一个函数输入.有时候一个复杂问题,我们拆分成很多个步骤函数,这些函数组合 ...
- windows常用快捷键和dos命令
windows常用快捷键 Ctrl+C (复制) Ctrl+V (粘贴) Ctrl+S (保存) Ctrl+A (全选) Ctrl+Z (撤销) Alt+F4 (关闭当前窗口) shift+Delet ...
- ansible的Ad-hoc命令
本文主要介绍了ansible的Ad-hoc命令. Ansible提供两种方式去完成任务,一是 ad-hoc 命令,一是写 Ansible playbook.前者可以解决一些简单的任务, 后者解决较复杂 ...