WPF常用样式配置

2021-07-19 16:00:23 浏览数 (1)

窗口

边界阴影

代码语言:javascript复制
<Window.Effect>
    <DropShadowEffect
        BlurRadius="10"
        Direction="80"
        ShadowDepth="0"
        Color="#f3f3f3" />
</Window.Effect>

Border

内部裁剪的Border

代码语言:javascript复制
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ZJClassTool.Views
{
    public class ZClippingBorder : Border
    {
        private object _oldClip;

        protected override void OnRender(DrawingContext dc)
        {
            OnApplyChildClip();
            base.OnRender(dc);
        }

        public override UIElement Child
        {
            get => base.Child;
            set
            {
                if (Child != value)
                {
                    if (Child != null)
                    {
                        Child.SetValue(ClipProperty, _oldClip);
                    }

                    if (value != null)
                    {
                        _oldClip = value.ReadLocalValue(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 = Child;
            if (child != null)
            {
                double top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight);
                double bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight);
                double max = Math.Max(top, bottom);
                Size size = RenderSize;
                double width = size.Width - (BorderThickness.Left   BorderThickness.Right);
                double height = size.Height - (BorderThickness.Top   BorderThickness.Bottom);
                Geometry result = new RectangleGeometry(new Rect(0, 0, width, height), max, max);
                double halfWidth = width / 2;
                double halfHeight = height / 2;

                if (CornerRadius.TopLeft == 0)
                {
                    result = new CombinedGeometry(
                        GeometryCombineMode.Union,
                        result,
                        new RectangleGeometry(new Rect(0, 0, halfWidth, halfHeight))
                    );
                }

                if (CornerRadius.TopRight == 0)
                {
                    result = new CombinedGeometry(GeometryCombineMode.Union, result, new RectangleGeometry
                (new Rect(halfWidth, 0, halfWidth, halfHeight)));
                }

                if (CornerRadius.BottomLeft == 0)
                {
                    result = new CombinedGeometry
                  (GeometryCombineMode.Union, result, new RectangleGeometry
                  (new Rect(0, halfHeight, halfWidth, halfHeight)));
                }
                if (CornerRadius.BottomRight == 0)
                {
                    result = new CombinedGeometry
                  (
                GeometryCombineMode.Union,
                result,
                new RectangleGeometry(new Rect(halfWidth, halfHeight, halfWidth, halfHeight))
                );
                }
                child.Clip = result;
            }
        }
    }
}

调用方式

引用

代码语言:javascript复制
xmlns:util="clr-namespace:ZJClassTool.Utils"

使用

代码语言:javascript复制
<Views:ZClippingBorder
                       Background="#fafafa"
                       BorderBrush="#409DFE"
                       BorderThickness="2"
                       CornerRadius="10">
</Views:ZClippingBorder>

0 人点赞