簡體   English   中英

WPF Dashed Border Control

[英]WPF Dashed Border Control

我想創建一個繼承自Border的控件,並且只允許我特定一個StrokeDashArray來破壞邊界線。

我不想使用'谷歌'建議的黑客,即矩形等,因為我想要邊境控制給予的靈活性。

但是,我沒有創建自定義控件的經驗,也不知道從哪里開始?

你能指出我正確的方向嗎?

謝謝!

仍然不是最佳的,但如何使用Matt Hamilton作為VisualBrush的鏈接解決方案

使用VisualBrush與虛線RectangleSolidColorBrush

在此輸入圖像描述

<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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

對不起,這有點晚了,但這是使用StrokeDashArray屬性的WPF解決方案。

ellipse Ellipse = new Ellipse();
/*code to change ellipse size, margin, color, etc*/
ellipse.StrokeDashArray=new DoubleCollection(new double[] {4, 3})
//First number is the dash length, second number the dash gap

我意識到這是c#代碼而不是XML,但屬性仍然相同。 如果您想要更多地控制破折號,請使用此處的控件的其他“Stroke”屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM