WPF Dashed Border Control

I want to create a control that inherits from Border, and just allows me to specify a StrokeDashArray to cross out the border.

I do not want to use the "google" suggested hacks, i.e. rectangles, etc., as I want flexibility to provide border controls.

However, I have no experience creating custom controls and don’t know where to start?

Could you point me in the right direction

Thanks!

+6
source share
2 answers

Still not optimal, but what about using the solution from Matt Hamilton's link as VisualBrush

Comparison using VisualBrush with dashed Rectangle and SolidColorBrush

enter image description here

 <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(); } } 
+12
source

Sorry it's a little late, but here is a solution for WPF using the StrokeDashArray property.

 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 

I understand that this is C # code, not XML, but the property remains the same. If you want more control over your dashes, use the other "Stroke" properties for the controls found here .

0
source

Source: https://habr.com/ru/post/894620/


All Articles