[.NET/WPF] 设置按钮, 以及其他任何包含边框的控件的圆角

这篇具有很好参考价值的文章主要介绍了[.NET/WPF] 设置按钮, 以及其他任何包含边框的控件的圆角。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在 WPF 中, 按钮包含一个 “边框”, 很多时候需要设置按钮的圆角, 但是按钮并没有提供一个属性用来设置边框圆角.

下面以按钮为例, 列举几种常用的设置圆角的方式.

通过附加属性

定义一个附加属性, 然后在各个地方就能直接方便的使用了, 下面是实际使用方式:

<Button utils.BorderUtils.CornerRadius="3"/>

接下来是具体实现代码, 首先是一些工具方法:

using System.Windows.Media;

namespace System.Windows
{
    public static class CommonUtils
    {
        public static void RunOnFirstLoaded(this FrameworkElement element, EventHandler handler)
        {
            void Once(object? sender, RoutedEventArgs e)
            {
                element.Loaded -= Once;
                handler.Invoke(sender, e);
            }

            if (element.IsLoaded)
                handler.Invoke(element, EventArgs.Empty);
            else
                element.Loaded += Once;
        }

        public static TElement? GetElementFromVisualTree<TElement>(this FrameworkElement control) where TElement : FrameworkElement
        {
            if (control is TElement ele)
                return ele;

            int childrenCount = VisualTreeHelper.GetChildrenCount(control);
            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(control, i);
                if (child is TElement eleChild)
                    return eleChild;
            }

            return null;
        }
    }
}

然后是 BorderUtils 这个类, 在其中定义 CornerRadius 附加属性:

using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace System.Windows
{
    public static class BorderUtils
    {
        [AttachedPropertyBrowsableForType(typeof(FrameworkElement))]
        public static CornerRadius GetCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(CornerRadiusProperty);
        }

        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }

        // Using a DependencyProperty as the backing store for CornerRadius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(BorderUtils), new PropertyMetadata(new CornerRadius(), CornerRadiusChanged));

        private static void CornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not FrameworkElement ele)
                return;

            ele.RunOnFirstLoaded((s, _e) =>
            {
                if (CommonUtils.GetElementFromVisualTree<Border>(ele) is not Border border)
                    return;

                border.CornerRadius = (CornerRadius)e.NewValue;
            });
        }
    }
}

通过资源

直接在 Button 节点下添加一个 Border 的样式资源, 然后 Button 中的 Border 就会应用这个样式.

<Button>
  <Button.Resources>
    <Style TargetType="Border">
      <Setter Property="CornerRadius" Value="3" />
    </Style>
  </Button.Resources>
</Button>

通过模板

很麻烦的一种方式, 不推荐文章来源地址https://www.toymoban.com/news/detail-689422.html

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

到了这里,关于[.NET/WPF] 设置按钮, 以及其他任何包含边框的控件的圆角的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • CSS按钮-跑马灯边框

    思路很简单,实现方法有很多很多。但是大体思路与实现方法都类似:渐变色 + 动画,主要区别在动画的具体实现

    2024年02月11日
    浏览(38)
  • 微信小程序button按钮去除边框

    今天在小程序开发中想要去除按钮的边框,直接使用 border: none 不起作用,选择器后加一个::after伪元素才行。 例: button:: after{ border: none }

    2024年02月06日
    浏览(39)
  • 微信小程序button按钮怎么去掉边框

    代码: 结果如下: 在css样式里写入: 即可解决

    2024年04月23日
    浏览(40)
  • Simple WPF:实现一个透明、无边框、鼠标穿透的WPF窗体

    一个自定义WPF窗体的解决方案,借鉴了吕毅老师的WPF制作高性能的透明背景的异形窗口一文,并在此基础上增加了鼠标穿透的功能。可以使得透明窗体的鼠标事件穿透到下层,在下层窗体中响应。这一功能在开发一些截图工具,直播、会议标注工具的时候会有比较多的应用,

    2024年02月09日
    浏览(80)
  • 微信小程序button按钮去除边框去除背景色

    button边框 去除button边框 在button上添加plain=“true” 在css中添加button.avatar-wrapper {background: none}用于去除button背景色 在css中添加button.avatar-wrapper[plain]{ border:0 }用于去除button边框

    2024年02月06日
    浏览(35)
  • [C# WPF] 如何给控件添加边框(Border)?

    在WPF中,可以使用边框控件或者边框属性来为控件添加边框。 以下是两种常见的方法: WPF中的Border控件用于为其他控件添加边框效果。它是一个容器控件,可以包含一个子元素,并为其提供边框、背景和填充等装饰效果。 以下是Border控件的一些主要属性: BorderBrush:用于定

    2024年02月20日
    浏览(27)
  • [ Windows 10 ] 任务栏按钮不显示正在打开的窗口了(打开任何程序任务栏图标按钮都不显示)

    系统是Windows 10 professional版本,在一次突然开机后,发现点开程序后,在任务栏什么都不显示,任务栏按钮和图标状态均不显示了,但是程序在运行。 当时兄弟们我心态直接蹦了啊,100万个草泥马根本停不下来,口里不断重复学习英文单词: what\\\'s the f**king going on? 异常情况如

    2024年02月04日
    浏览(37)
  • SQL Server 中语句显示红色波浪线,提示对象/列名无效解决方法(无其他任何显式错误的情况)

    拼写 名称等都没有出现任何错误时,依旧报错:对象/列名无效 解决步骤: 工具-选项-文本编辑器-Transact-SQL-IntelliSense 重新勾选 启用 IntelliSense 或者将 最大脚本大小改为 无限制 当然 工具卡了也可能会失效,进行 刷新 保存 或者重启 配合操作尝试调整。

    2024年04月23日
    浏览(46)
  • .NET 创建无边框的跨平台应用

    在创建了 Photino 应用程序以后我们发现它自带了一个标题栏,并且非常丑,我们现在要做的就是去掉这个很丑的自带标题栏,并且自定义一个更好看的,下面我们将用 Masa Blazor 提供的模板去进行实战。 安装 Masa Blazor 提供的 rc2 的模板 打开VS2022 = 新建项目 搜索到一下类别! 然

    2024年02月06日
    浏览(45)
  • WPF:自定义按钮模板

    1.WPF:自定义按钮模板 自定义封装的按钮属性可写在Button.Template中 Background=\\\"{TemplateBinding Background}\\\"中的TemplateBinding代表使用按钮本身所使用的背景颜色 不在样式内修改背景颜色 例如: 2.通过事件处理改变属性 1.首先先定义按钮名称 Border x:Name=“button” 2.在 ControlTemplate.Trigger

    2024年02月08日
    浏览(61)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包