我想创建一个从Border继承的控件,它可以简单地允许我指定StrokeDashArray以虚线绘制边框线。
我不想使用'google'建议的hack,例如矩形等,因为我想要边框控件给出的灵活性。
但是,我没有创建自定义控件的经验,也不知道从哪里开始?
你可以指点我一下吗?
谢谢!
我不想使用'google'建议的hack,例如矩形等,因为我想要边框控件给出的灵活性。
但是,我没有创建自定义控件的经验,也不知道从哪里开始?
你可以指点我一下吗?
谢谢!
虽然还不是最优解,但尝试使用来自链接的Matt Hamilton的解决方案作为VisualBrush
使用带有虚线Rectangle
和SolidColorBrush
的VisualBrush
进行比较
<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();
}
}
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