2013-06-05 41 views
14

我知道我可以创建一个矩形虚线边框或不同的笔画粗细的不同侧面边框:WPF在两侧有不同笔划粗细的矩形或带有虚线笔划的边框?

 <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不能用于实现这个..可以吗?

回答

20

试试这个:

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

它的边界,所以当放在网格内部它将使用可用空间,并且可以为每一边设置不同的宽度,它使用矩形作为视觉画笔,因此您可以轻松地将边框设置为虚线。

enter image description here

+0

好的!我从来不知道我可以像这样定义自己的画笔! – markmnl

1

哈克解决方案,但它的工作原理是覆盖你想隐藏的虚线矩形的一面:

  <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