当前位置: 首页 > ops >正文

WPF按钮Radius化

在WPF中,可以通过以下几种方式为按钮添加圆角(Radius化):

方法一:通过自定义按钮模板

在XAML中,可以通过自定义按钮的ControlTemplate来设置圆角。具体步骤如下:

  1. 定义一个ControlTemplate,并在其中使用Border控件。
  2. Border控件设置CornerRadius属性。

示例代码:

<Button Content="圆角按钮" Width="120" Height="40"><Button.Template><ControlTemplate TargetType="{x:Type Button}"><Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></ControlTemplate></Button.Template>
</Button>

方法二:使用全局样式

如果需要为多个按钮设置统一的圆角样式,可以定义一个全局样式。

示例代码:

<Window.Resources><Style x:Key="RoundButtonStyle" TargetType="{x:Type Button}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></ControlTemplate></Setter.Value></Setter></Style>
</Window.Resources><Button Content="圆角按钮" Width="120" Height="40" Style="{StaticResource RoundButtonStyle}"/>

方法三:直接在Border控件中设置

如果按钮的内容是通过Border控件包装的,可以直接在Border控件中设置CornerRadius

示例代码:

<Button Content="圆角按钮" Width="120" Height="40"><Button.Resources><Style TargetType="{x:Type Border}"><Setter Property="CornerRadius" Value="10"/></Style></Button.Resources>
</Button>

方法四:通过代码动态设置

如果需要在代码中动态创建按钮并设置圆角,可以通过以下方式:

Button myButton = new Button
{Content = "动态圆角按钮",Width = 120,Height = 40
};var buttonTemplate = new ControlTemplate(typeof(Button))
{VisualTree = new FrameworkElementFactory(typeof(Border)){SetValue(Border.CornerRadiusProperty, new CornerRadius(10)),SetValue(Border.BackgroundProperty, Brushes.LightBlue),SetValue(Border.ChildProperty, new FrameworkElementFactory(typeof(ContentPresenter)){SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center),SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center)})}
};myButton.Template = buttonTemplate;

以上方法可以根据实际需求选择使用,实现按钮的圆角效果。

http://www.xdnf.cn/news/8531.html

相关文章:

  • DAY01:【ML 第二弹】高等数学
  • Java 函数式接口(Functional Interface)
  • Disruptor—2.并发编程相关简介
  • HarmonyOS实战:高德地图定位功能完整流程详解
  • 《Spark/Flink/Doris离线实时数仓开发》目录
  • Linux目录介绍+Redis部署(小白篇)
  • 基于大模型的髋关节骨关节炎预测与治疗方案研究报告
  • client.chat.completions.create方法参数详解
  • 指令集架构、微架构、厂商对应关系
  • 浏览器强缓存还未过期,但服务器资源已经变了怎么办?
  • 打破产品思维--启示录:打造用户喜欢的产品--实战6
  • 动静态库--
  • 软件开发MVC三层架构杂谈
  • Android-OkHttp与Retrofit学习总结
  • 【疑难杂症】Vue前端下载文件无法打开 已解决
  • WebAssembly:开启跨平台高性能编程的新时代
  • 游戏引擎学习第309天:用于重叠检测的网格划分
  • 后端开发概念
  • 独立机构软件第三方检测:流程、需求分析及电商软件检验要点?
  • SystemUtils:你的Java系统“探照灯“——让环境探测不再盲人摸象
  • SQL每日一练(3)
  • XOR符号
  • esp32+IDF V5.1.1版本编译freertos报错
  • 机器学习——支持向量机(SVM)
  • 怎么开发一个网络协议模块(C语言框架)之(四) 信号量初始化
  • 【Java Web】3.SpringBootWeb请求响应
  • Spring 框架的JDBC 模板技术
  • 使用Python控制Arduino——入门与实战
  • Axure酒店管理系统原型
  • 【如何做好一份技术文档?】用Javadoc与PlantUML构建高质量技术文档(API文档自动化部署)