WPF 关于圆角的制作

简介: 原文:WPF 关于圆角的制作1、使用Boder(一般情况): 设置CornerRadius属性   ...   2、创建ClippingBorder类: View Code using System; using System.
原文: WPF 关于圆角的制作

1、使用Boder(一般情况):

设置CornerRadius属性

 <Border x:Name="border" CornerRadius="20">

...

</Border>

 

2、创建ClippingBorder类:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
 
namespace Shgbit.Controls {
    /// <Remarks>
    ///     As a side effect ClippingBorder will surpress any databinding or animation of 
    ///         its childs UIElement.Clip property until the child is removed from ClippingBorder
    /// </Remarks>
    public class ClippingBorder : Border {
        protected override void OnRender(DrawingContext dc) {
            OnApplyChildClip();
            base.OnRender(dc);
        }
 
        public override UIElement Child {
            get {
                return base.Child;
            }
            set {
                if (this.Child != value) {
                    if (this.Child != null) {
                        // Restore original clipping
                        this.Child.SetValue(UIElement.ClipProperty, _oldClip);
                    }
 
                    if (value != null) {
                        _oldClip = value.ReadLocalValue(UIElement.ClipProperty);
                    } else {
                        // If we dont set it to null we could leak a Geometry object
                        _oldClip = null;
                    }
 
                    base.Child = value;
                }
            }
        }
 
        protected virtual void OnApplyChildClip() {
            UIElement child = this.Child;
            if (child != null) {
                _clipRect.RadiusX = _clipRect.RadiusY = Math.Max(0.0, this.CornerRadius.TopLeft - (this.BorderThickness.Left * 0.5));
                Rect rect = new Rect(this.RenderSize);
                rect.Height -= (this.BorderThickness.Top + this.BorderThickness.Bottom);
                rect.Width -= (this.BorderThickness.Left + this.BorderThickness.Right);
                _clipRect.Rect = rect;
                child.Clip = _clipRect;
            }
        }
 
        public void Update() { OnApplyChildClip(); }
 
        private RectangleGeometry _clipRect = new RectangleGeometry();
        private object _oldClip;
    }
}

用法:
需应用xmlns:control="你的命名空间"

<control:ClippingBorder CornerRadius="20">
    <StackPanel Background="Yellow">
        <Label>Hello World</Label>
    </StackPanel>
</control:ClippingBorder>

 

3、使用Clip(通过路径):

<Border Width="300" Height="100">
    <Border.Clip>
        <PathGeometry>
            <PathGeometry.Figures>
                <PathFigure StartPoint="0,0" IsClosed="True">
                    <LineSegment Point="300,0" />
                    <LineSegment Point="300,80" />
                    <ArcSegment Point="280,100" Size="20,20" SweepDirection="Clockwise"/>
                    <LineSegment Point="0,100" />
                </PathFigure>
            </PathGeometry.Figures>
        </PathGeometry>
    </Border.Clip>
    <StackPanel Background="Yellow">
        <Label>Hello World</Label>
    </StackPanel>
</Border>

 

目录
相关文章
|
12月前
|
C#
WPF空心圆角combox
WPF空心圆角combox
129 0
|
C#
WPF 创建无边框的圆角窗口
原文:WPF 创建无边框的圆角窗口 如题所述,在WPF中要创建一个没有边框且为圆角的窗体,有如下几步工作要进行: 第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为True,...
2613 0
|
C#
WPF 圆角textbox
原文:WPF 圆角textbox 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a771948524/article/details/9245965 ...
1230 0
|
C#
WPF圆角按钮与触发颜色变化
原文:WPF圆角按钮与触发颜色变化 ...
1555 0
|
C#
WPF 圆角输入框
原文:WPF 圆角输入框 今天打算来做一个圆角的输入框 默认输入框:   这个输入框不好看,并且在XP 跟 WIN 7  WIN10 效果 都不太一样   我们今天不用模板的方式,而是 最简单的方式 来实现 圆角 输入框;     ------------------------...
1344 0
|
C# 前端开发
[原译]WPF绘制圆角多边形
原文:[原译]WPF绘制圆角多边形 介绍 最近,我发现我需要个圆角多边形。而且是需要在运行时从用户界面来绘制。WPF有多边形。但是不支持圆角。我搜索了一下。也没找到可行的现成例子。于是就自己做吧。本文描述了圆角多边形的实现,也包括如何用在你的项目里。
1519 0
|
C#
WPF换肤之一:创建圆角窗体
原文:WPF换肤之一:创建圆角窗体     我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感。这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文中有任何错误或者不足,还请指出。
1336 0
|
2月前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
177 0
|
2月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
88 1