C# 侧面具有不同笔画粗细的WPF矩形或带有虚线笔画的边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16931984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Rectangle with different stroke thickness on sides or Border with dashed stroke?
提问by markmnl
I know I can create a dashed border with a rectangle or a border with different stroke thickness for different sides:
我知道我可以创建一个带有矩形的虚线边框或不同边具有不同笔触粗细的边框:
<StackPanel Orientation="Horizontal">
<Rectangle Stroke="Green" StrokeThickness="2" StrokeDashArray="4 2" Fill="LightGreen" Height="64" Width="32" Margin="5"/>
<Border BorderBrush="Green" BorderThickness="2,2,2,0" Background="LightGreen" Height="64" Width="32" Margin="5" />
</StackPanel>


Is there anyway I can achieve both:
无论如何我可以同时实现:


?
?
UPDATE: This needs to fill the space in it's parent (unlike my example with fixed sizes), e.g. a Grid - so a DrawingGeometry which has fixed sizes and my own Pen cannot be used to achieve this.. can it?
更新:这需要填充其父级中的空间(与我的固定大小示例不同),例如网格 - 因此具有固定大小的 DrawingGeometry 和我自己的 Pen 不能用于实现此目的.. 可以吗?
采纳答案by lisp
Try this:
尝试这个:
<Border BorderThickness="4,4,4,0" Background="LightGreen">
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle
Stroke="Green" Fill="LightGreen"
StrokeDashArray="4 2"
StrokeThickness="4"
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>
It's border, so when put inside of grid it will use the available space and you can set different width for every side, it uses rectangle for visual brush, so you can easily set the borders to dashed.
它是边框,所以当放在网格里面时,它会使用可用空间,你可以为每一边设置不同的宽度,它使用矩形作为视觉画笔,所以你可以轻松地将边框设置为虚线。


回答by markmnl
A hacky solution but it works is to cover the side of the dashed Rectangle you want hidden:
一个hacky解决方案,但它的工作原理是覆盖你想要隐藏的虚线矩形的一侧:
<Grid Width="100" Height="100">
<Rectangle Stroke="Green" StrokeThickness="4" StrokeDashArray="4 2" Fill="LightGreen" Margin="10"/>
<Rectangle StrokeThickness="0" Height="4" Margin="10" VerticalAlignment="Bottom" Fill="LightGreen"/>
</Grid>



