如何在 WPF 中实现虚线或点线边框?
我有一个 ListViewItem
,我正在对其应用 Style
,并且我想将一条灰色虚线作为底部 Border
。
我怎样才能在 WPF 中做到这一点?我只能看到纯色画笔。
I have a ListViewItem
that I am applying a Style
to and I would like to put a dotted grey line as the bottom Border
.
How can I do this in WPF? I can only see solid color brushes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这在我们的应用程序中效果很好,允许我们使用真正的边框而不是乱用矩形:
请注意,视口决定线条中虚线的大小。在本例中,它生成八像素的虚线。 Viewport="0,0,4,4" 会给你四个像素的破折号。
This worked great in our application, allowing us to use a real Border and not mess around with Rectangles:
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.
您可以使用矩形创建点线或短划线,如下面的代码所示
开始使用并根据您的场景自定义列表视图
You can create a dotted or dashes line using a rectangle like in the code below
Get started with this and customize your listview according to your scenario
聚会有点晚了,但以下解决方案对我有用。它比其他两种解决方案稍微简单/更好:
A bit late to the party, but the following solution worked for me. It is slightly simpler/better than both other solutions:
沙姆尔
Xaml
我们的团队最近收到了这个需求,并通过创建自定义控件
DashedBorder
来解决它,它扩展了Border
并添加了虚线边框功能。它有 3 个新的依赖属性
UseDashedBorder
(bool)DashedBorderBrush
(Brush)StrokeDashArray
(DoubleCollection)像这样使用
并产生这样的结果
当
UseDashedBorder
设置为true
时,它将创建一个带有 2 个矩形的VisualBrush
并将其设置为BorderBrush
(这就是为什么我们需要一个额外的属性来表示实际BorderBrush
的颜色)。第一个是创建虚线,第二个是用边框的背景填充空白。它将
Rectangle
dashing 属性映射到DashedBorder
属性,如下所示StrokeDashArray
=>StrokeDashArray
Stroke
=>;DashedBorderBrush
StrokeThickness
=>;BorderThickness.Left
RadiusX
=>CornerRadius.TopLeft
RadiusY
=>CornerRadius.TopLeft
宽度
=>实际宽度
高度
=>ActualHeight
DashedBorder.cs
Our team got this as a requirement lately and we solved it by creating a custom control,
DashedBorder
which extendsBorder
and adds the dashed border feature.It has 3 new dependency properties
UseDashedBorder
(bool)DashedBorderBrush
(Brush)StrokeDashArray
(DoubleCollection)Usable like this
And produces a result like this
When
UseDashedBorder
is set totrue
it will create aVisualBrush
with 2 rectangles and set that asBorderBrush
(that's why we need an extra property for the color of the actualBorderBrush
). The first one is to create the dashing and the second of is to fill in the gaps with theBackground
of the border.It maps the
Rectangle
dashing properties to theDashedBorder
properties like thisStrokeDashArray
=>StrokeDashArray
Stroke
=>DashedBorderBrush
StrokeThickness
=>BorderThickness.Left
RadiusX
=>CornerRadius.TopLeft
RadiusY
=>CornerRadius.TopLeft
Width
=>ActualWidth
Height
=>ActualHeight
DashedBorder.cs
正在处理用户控件......
我一直在尝试制作行进蚂蚁边界的故事板。由于没有交互,带有矩形和文本的基本网格工作正常。当尝试将按钮放入网格内时,矩形或按钮都可见,但两者都不可见。
来自另一篇文章:
高级 XAML 动画效果。脉冲、行进的蚂蚁、旋转。警报
使用 dotNet 的 VisualBrush 解决方案将矩形移动到边框,内部有一个按钮。这非常有效。
Working on a user control....
I have been trying a storyboard for a marching ants border. The basic grid with a rectangle and text works fine since there is no interaction. When trying to put a button inside the grid, then either the rectangle or button is visible but never both of them.
From another post:
Advanced XAML Animation effects. Pulse, Marching ants, Rotations. Alerts
Using dotNet's solution for the VisualBrush shifted the rectangle to the border with a button inside. This worked perfectly.
如果您正在寻找像素完美的虚线
https://referencesource.microsoft.com/#WindowsBase/Shared/MS/内部/DoubleUtil.cs
If you are looking for pixel perfect dashed lines
https://referencesource.microsoft.com/#WindowsBase/Shared/MS/Internal/DoubleUtil.cs