c# – WPF Dashed Border Control

前端之家收集整理的这篇文章主要介绍了c# – WPF Dashed Border Control前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个继承自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();
    }
}

猜你在找的C#相关文章