WPF制作只有部分圆角的矩形

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">wpf中原生的矩形可以通过设置RadiusX和RadiusY来实现圆角矩形的效果,但这样的矩形四个角都是圆角。</span>

但有时候我们需要一个矩形只有上面的有圆角,下面是直角。


解决办法就是自定义一个类继承图形类(Shape),定义相应的依赖属性,然后重写绘制方法即可。

具体代码如下:

public class PartiallyRoundedRectangle : Shape {
    public static readonly DependencyProperty RadiusXProperty;
    public static readonly DependencyProperty RadiusYProperty;

    public static readonly DependencyProperty RoundTopLeftProperty;
    public static readonly DependencyProperty RoundTopRightProperty;
    public static readonly DependencyProperty RoundBottomLeftProperty;
    public static readonly DependencyProperty RoundBottomRightProperty;

    public int RadiusX {
        get { return (int)GetValue(RadiusXProperty); }
        set { SetValue(RadiusXProperty, value); }
    }

    public int RadiusY {
        get { return (int)GetValue(RadiusYProperty); }
        set { SetValue(RadiusYProperty, value); }
    }

    public bool RoundTopLeft {
        get { return (bool)GetValue(RoundTopLeftProperty); }
        set { SetValue(RoundTopLeftProperty, value); }
    }

    public bool RoundTopRight {
        get { return (bool)GetValue(RoundTopRightProperty); }
        set { SetValue(RoundTopRightProperty, value); }
    }

    public bool RoundBottomLeft {
        get { return (bool)GetValue(RoundBottomLeftProperty); }
        set { SetValue(RoundBottomLeftProperty, value); }
    }

    public bool RoundBottomRight {
        get { return (bool)GetValue(RoundBottomRightProperty); }
        set { SetValue(RoundBottomRightProperty, value); }
    }

    static PartiallyRoundedRectangle() {
        RadiusXProperty = DependencyProperty.Register
            ("RadiusX", typeof(int), typeof(PartiallyRoundedRectangle));
        RadiusYProperty = DependencyProperty.Register
            ("RadiusY", typeof(int), typeof(PartiallyRoundedRectangle));

        RoundTopLeftProperty = DependencyProperty.Register
            ("RoundTopLeft", typeof(bool), typeof(PartiallyRoundedRectangle));
        RoundTopRightProperty = DependencyProperty.Register
            ("RoundTopRight", typeof(bool), typeof(PartiallyRoundedRectangle));
        RoundBottomLeftProperty = DependencyProperty.Register
            ("RoundBottomLeft", typeof(bool), typeof(PartiallyRoundedRectangle));
        RoundBottomRightProperty = DependencyProperty.Register
            ("RoundBottomRight", typeof(bool), typeof(PartiallyRoundedRectangle));
    }

    public PartiallyRoundedRectangle() {
    }

    protected override Geometry DefiningGeometry {
        get {
            Geometry result = new RectangleGeometry
            (new Rect(0, 0, base.Width, base.Height), RadiusX, RadiusY);
            double halfWidth = base.Width / 2;
            double halfHeight = base.Height / 2;

            if (!RoundTopLeft)
                result = new CombinedGeometry
            (GeometryCombineMode.Union, result, new RectangleGeometry
            (new Rect(0, 0, halfWidth, halfHeight)));
            if (!RoundTopRight)
                result = new CombinedGeometry
            (GeometryCombineMode.Union, result, new RectangleGeometry
            (new Rect(halfWidth, 0, halfWidth, halfHeight)));
            if (!RoundBottomLeft)
                result = new CombinedGeometry
            (GeometryCombineMode.Union, result, new RectangleGeometry
            (new Rect(0, halfHeight, halfWidth, halfHeight)));
            if (!RoundBottomRight)
                result = new CombinedGeometry
            (GeometryCombineMode.Union, result, new RectangleGeometry
            (new Rect(halfWidth, halfHeight, halfWidth, halfHeight)));

            return result;
        }
    }
}


Xaml中使用方法如下:


<custom:PartiallyRoundedRectangle RoundTopLeft="True" 
    RoundBottomRight="True" RadiusX="20" RadiusY="20" 
    Fill="LightBlue" Height="112" Width="200" />

通过设置RoundTopLeft、RoundTopRight、RoundBottomLeft、RoundBottonRight这四个属性来设置相应的圆角。


参考连接

WPF-PartiallyRoundedRectangle-Choose-Which-Corners






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
WPF 圆角矩形展开折叠是指在使用 WPF(Windows Presentation Foundation)技术开发界面时,使用圆角矩形元素进行展开和折叠效果的实现。 首先,圆角矩形是一种特殊形状的矩形,其四个角都是圆角的,通过设置它的 CornerRadius 属性来实现。在 WPF 中,可以使用 Border、Rectangle 或其他自定义控件来创建圆角矩形。 要实现圆角矩形的展开折叠效果,可以使用 VisualStateManager 的 VisualStateGroup 和 VisualState 来定义不同状态下的外观和行为。可以创建两个不同的 VisualState,比如 Expanded(展开)和 Collapsed(折叠),分别对应圆角矩形的展开和折叠状态。在这两个 VisualState 中,可以设置圆角矩形的不同属性,如宽度、高度、颜色以及 CornerRadius。 接着,可以使用触发器 Trigger 来触发状态的切换。在 WPF 中,可以使用属性触发器 PropertyTrigger 或事件触发器 EventTrigger。可以在展开和折叠操作对应的事件中,设置 VisualStateGroup 的 CurrentState 属性来切换状态。比如,在点击一个按钮时触发的事件中,可以判断当前状态是展开还是折叠,然后通过修改 VisualStateGroup 的 CurrentState 进行状态切换。 最后,在 XAML 文件中,可以将圆角矩形和触发器定义在控件的模板或样式中,以实现对整个界面或特定控件的展开折叠效果。在定义的样式或模板中,可以设置触发器、VisualStateException 和圆角矩形的属性,使其满足展开和折叠的需求。 总之,使用 WPF 技术开发界面时,可以通过定义圆角矩形的展开和折叠状态、设置触发器以及修改相关属性来实现圆角矩形的展开折叠效果。通过这种方式,可以为界面添加更多的交互性和美观性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值