WPF矩形在两侧具有不同的笔划粗细或具有虚线笔划的边框?

时间:2013-06-05 05:10:30

标签: c# wpf xaml wpf-4.0

我知道我可以为不同的边创建一个带有矩形或不同笔触粗细边框的虚线边框:

        <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>

enter image description here

无论如何,我可以同时实现这两个目标:

enter image description here

更新:这需要填充其父级空间(与我的固定大小的示例不同),例如一个网格 - 所以一个具有固定大小的DrawingGeometry和我自己的Pen不能用来实现这个......可以吗?

2 个答案:

答案 0 :(得分:22)

试试这个:

<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>

它的边框,因此当放入网格内部时,它将使用可用空间,您可以为每一面设置不同的宽度,它使用矩形进行可视化刷,因此您可以轻松地将边框设置为虚线。 / p>

enter image description here

答案 1 :(得分:1)

一个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>

enter image description here