WPF矩形在两侧具有不同的行程厚度或具有虚线行程的边界?

mar*_*mnl 16 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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

无论如何,我可以实现两者:

在此输入图像描述

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

lis*_*isp 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>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述