C# 虚线边界控制
我想创建一个从Border继承的控件,它只允许我指定一个StrokeDashArray来划线 我不想使用“谷歌”建议的黑客,如矩形等,因为我希望边界控制提供灵活性 但是,我没有创建自定义控件的经验,也不知道从哪里开始 你能给我指一下正确的方向吗C# 虚线边界控制,c#,wpf,controls,styles,C#,Wpf,Controls,Styles,我想创建一个从Border继承的控件,它只允许我指定一个StrokeDashArray来划线 我不想使用“谷歌”建议的黑客,如矩形等,因为我希望边界控制提供灵活性 但是,我没有创建自定义控件的经验,也不知道从哪里开始 你能给我指一下正确的方向吗 谢谢 仍然不是最优的,但是使用Matt Hamilton的链接中的解决方案作为可视化画笔怎么样 <Border BorderThickness="3,2,1,0" CornerRadius="10"> <Border.Borde
谢谢 仍然不是最优的,但是使用Matt Hamilton的链接中的解决方案作为
可视化画笔怎么样
<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>
使用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>
抱歉,这有点晚了,但是这里有一个使用属性的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,但属性仍然是一样的。如果您想对虚线进行更多控制,请对找到的控件使用其他“笔划”属性。边框不是控件,而是框架元素。最好用属性来解决这个问题。这里建议的解决方案有什么问题?如果不允许我只绘制边框的某些边,即设置strokethickness=“1,0,0,1”仍然会绘制矩形,并且不会遗漏顶部的右侧