在 WPF 中创建条纹画笔

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

Create stripe brush in WPF

wpfbrush

提问by Metro

How can I create a brush (DrawingBrush) for stripes as shown in the blog:

如何为条纹创建画笔(DrawingBrush),如博客中所示:

http://blog.pixelingene.com/2008/09/quick-tip-to-get-a-striped-background/

http://blog.pixelingene.com/2008/09/quick-tip-to-get-a-striped-background/

I can't use it because it uses scale transform, which means if the UI element is small, the stripes are pretty much not visible or too close together.

我不能使用它,因为它使用缩放变换,这意味着如果 UI 元素很小,条纹几乎不可见或靠得太近。

I can't use image brush because I need to bind the colors.

我不能使用图像刷,因为我需要绑定颜色。

采纳答案by Lee Louviere

Creates downward 45 degree angle stripe pattern. Alter viewport to change size of stripes

创建向下 45 度角条纹图案。改变视口以改变条纹的大小

<DrawingBrush Stretch="UniformToFill" ViewportUnits="Absolute" Viewport="0,0,10,10" TileMode="Tile">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="Black">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup FillRule="Nonzero">
                            <PathGeometry>
                                <PathFigure StartPoint="0,0">
                                    <LineSegment  Point="100,0"/>
                                    <LineSegment  Point="100,100"/>
                                    <LineSegment  Point="0,100"/>
                                </PathFigure>
                            </PathGeometry>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FF404040">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup FillRule="Nonzero">
                            <PathGeometry>
                                <PathFigure StartPoint="0,0">
                                    <LineSegment  Point="25,0"/>
                                    <LineSegment  Point="100,75"/>
                                    <LineSegment  Point="100,100"/>
                                    <LineSegment  Point="75,100"/>
                                    <LineSegment  Point="0,25"/>
                                    <LineSegment  Point="0,0"/>
                                </PathFigure>
                                <PathFigure StartPoint="75,0">
                                    <LineSegment  Point="100,25"/>
                                    <LineSegment  Point="100,0"/>
                                </PathFigure>
                                <PathFigure StartPoint="0,75">
                                    <LineSegment  Point="25,100"/>
                                    <LineSegment  Point="0,100"/>
                                </PathFigure>
                            </PathGeometry>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

Alternatively you could bind the scale transform to the height and width of the control using multibinding. Then with a converter, you alter the scale to the max of height or width, then the stripes will remain the same size.

或者,您可以使用多重绑定将缩放变换绑定到控件的高度和宽度。然后使用转换器,将比例更改为高度或宽度的最大值,然后条纹将保持相同的大小。

回答by tom-englert

Just use MappingMode="Absolute":

只需使用 MappingMode="Absolute":

<LinearGradientBrush MappingMode="Absolute" x:Key="HatchBrush"  StartPoint="0,0" EndPoint="4,4" SpreadMethod="Repeat">
  <GradientStop Offset="0" Color="LightCoral"/>
  <GradientStop Offset="0.75" Color="LightCoral"/>
  <GradientStop Offset="0.75" Color="Gray"/>
  <GradientStop Offset="1" Color="Gray"/>
</LinearGradientBrush>