如何在 WPF 中实现虚线或虚线边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6195395/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:52:46  来源:igfitidea点击:

How can I achieve a dashed or dotted border in WPF?

wpfstylesborderlistviewitem

提问by dan

I have a ListViewItemthat I am applying a Styleto and I would like to put a dotted grey line as the bottom Border.

我有一个ListViewItem我正在申请的Style,我想把灰色虚线作为底部Border

How can I do this in WPF? I can only see solid color brushes.

我怎样才能在 WPF 中做到这一点?我只能看到纯色画笔。

回答by Rand Scullard

This worked great in our application, allowing us to use a real Border and not mess around with Rectangles:

这在我们的应用程序中效果很好,允许我们使用真正的边框而不是矩形:

<Border BorderThickness="1,0,1,1">
   <Border.BorderBrush>
      <DrawingBrush Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">
         <DrawingBrush.Drawing>
            <DrawingGroup>
               <GeometryDrawing Brush="Black">
                  <GeometryDrawing.Geometry>
                     <GeometryGroup>
                        <RectangleGeometry Rect="0,0,50,50" />
                        <RectangleGeometry Rect="50,50,50,50" />
                     </GeometryGroup>
                  </GeometryDrawing.Geometry>
               </GeometryDrawing>
            </DrawingGroup>
         </DrawingBrush.Drawing>
      </DrawingBrush>
   </Border.BorderBrush>

   <TextBlock Text="Content Goes Here!" Margin="5"/>
</Border>

Note that the Viewport determines the size of the dashes in the lines. In this case, it generates eight-pixel dashes. Viewport="0,0,4,4" would give you four-pixel dashes.

请注意,视口决定了线条中虚线的大小。在这种情况下,它会生成八像素的破折号。Viewport="0,0,4,4" 会给你四像素的破折号。

回答by biju

You can create a dotted or dashes line using a rectangle like in the code below

您可以使用如下代码中的矩形创建虚线或虚线

<Rectangle Stroke="#FF000000" Height="1" StrokeThickness="1" StrokeDashArray="4 4"
                                                       SnapsToDevicePixels="True"/>

Get started with this and customize your listview according to your scenario

开始使用它并根据您的场景自定义您的列表视图

回答by dotNET

A bit late to the party, but the following solution worked for me. It is slightly simpler/better than both other solutions:

聚会有点晚了,但以下解决方案对我有用。它比其他两种解决方案更简单/更好:

<Border BorderThickness="1">
  <Border.BorderBrush>
    <VisualBrush>
      <VisualBrush.Visual>
        <Rectangle StrokeDashArray="4 2" Stroke="Gray" StrokeThickness="1"
                  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>

  <TextBlock Text="Whatever" />
</Border>

回答by Dreia Ria

Xaml

xml

<Grid>
<Grid.RowDefinitions><RowDefinition Height="auto"/></Grid.RowDefinitions>
<Grid.ColumnDefinitions><ColumnDefinition Width="auto"/></Grid.ColumnDefinitions>
<Rectangle RadiusX="9" RadiusY="9" Fill="White" Stroke="Black" StrokeDashArray="1,2"/>
<TextBlock Padding = "4,2" Text="Whatever"/>
</Grid>