加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF Dashed Border Control

发布时间:2020-12-15 08:34:56 所属栏目:百科 来源:网络整理
导读:我想创建一个继承自Border的控件,并且只允许我特定一个StrokeDashArray来破坏边界线. 我不想使用’谷歌’建议的黑客,即矩形等,因为我想要边境控制给予的灵活性. 但是,我没有创建自定义控件的经验,也不知道从哪里开始? 你能指出我正确的方向吗? 谢谢! 解决
我想创建一个继承自Border的控件,并且只允许我特定一个StrokeDashArray来破坏边界线.

我不想使用’谷歌’建议的黑客,即矩形等,因为我想要边境控制给予的灵活性.

但是,我没有创建自定义控件的经验,也不知道从哪里开始?

你能指出我正确的方向吗?

谢谢!

解决方法

仍然不是最佳的,但如何使用Matt Hamilton作为VisualBrush的链接解决方案

使用VisualBrush与虚线矩形和SolidColorBrush进行比较

<Border BorderThickness="3,2,1,0" CornerRadius="10">
    <Border.BorderBrush>
        <VisualBrush>
            <VisualBrush.Visual>
                <Rectangle StrokeDashArray="1.0 1.0"
                           Stroke="Red"
                           StrokeThickness="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=BorderThickness,Converter={StaticResource ThicknessMaxConverter}}"
                           RadiusX="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=CornerRadius.TopRight}"
                           RadiusY="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=CornerRadius.BottomLeft}"
                           Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualWidth}"
                           Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualHeight}"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Border.BorderBrush>
</Border>

ThicknessMaxConverter

public class ThicknessMaxConverter : IValueConverter
{
    public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
    {
        Thickness thickness = (Thickness)value;
        double horizontalMax = Math.Max(thickness.Left,thickness.Right);
        double verticalMax = Math.Max(thickness.Top,thickness.Bottom);
        return Math.Max(horizontalMax,verticalMax);
    }
    public object ConvertBack(object value,System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读