如何在 WPF 中创建重复的对角线作为背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14665678/
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
How do I create repeating diagonal lines as a background in WPF?
提问by Jamie Nordmeyer
OK, I've wracked my brain, Google, and stackoverflow trying to figure this out, but just can't quite get it. I'm trying to use a DrawingBrush with a DrawingGroup to do two things to the background of my WPF application (latest version). One, I want to have a RadialGradientBrush to put a subtle gradient in my background. This part is working just fine. The second part that I'm trying to accomplish is that I want to also have repeating diagonal lines as part of that background. I know I could do it with an image, but would rather use geometries, as I'm trying to learn and master WPF. Here's what I have so far. The radial appears fine, but the lines do not. Any help would be appreciated.
好吧,我已经绞尽脑汁、谷歌和 stackoverflow 试图解决这个问题,但就是不能完全理解。我正在尝试使用带有 DrawingGroup 的 DrawingBrush 对我的 WPF 应用程序(最新版本)的背景做两件事。一,我想要一个 RadialGradientBrush 在我的背景中放置一个微妙的渐变。这部分工作得很好。我要完成的第二部分是我还希望将重复的对角线作为该背景的一部分。我知道我可以用图像来做,但宁愿使用几何图形,因为我正在尝试学习和掌握 WPF。这是我到目前为止所拥有的。径向看起来很好,但线条没有。任何帮助,将不胜感激。
<Style x:Key="WindowBackground" TargetType="Grid">
<Setter Property="Background">
<Setter.Value>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="#EE9D40" Offset="0"/>
<GradientStop Color="#BF8230" Offset="1"/>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<DrawingBrush TileMode="Tile" Stretch="None" Viewbox="0,0,1,1" Viewport="0,0,25,25" ViewportUnits="Absolute">
<DrawingBrush.RelativeTransform>
<TranslateTransform X="0" Y="0" />
</DrawingBrush.RelativeTransform>
<DrawingBrush.Drawing>
<GeometryDrawing Brush="#20FFFFFF" Geometry="M10,0 22,0 12,25 0,22 Z" />
</DrawingBrush.Drawing>
</DrawingBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Setter.Value>
</Setter>
</Style>
回答by Cédric Bignon
This trick should work.
这个技巧应该有效。
<LinearGradientBrush EndPoint="0,0" StartPoint="8,8"
MappingMode="Absolute" SpreadMethod="Repeat">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Black" Offset="0.1" />
<GradientStop Color="White" Offset="0.1" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
回答by Chris Zahrobsky
Try rotating a horizontal line brush to avoid the corner gaps:
<VisualBrush TileMode="Tile" Viewport="0,0,5,5" ViewportUnits="Absolute" Viewbox="0,0,5,5" ViewboxUnits="Absolute"> <VisualBrush.Visual> <Grid Background="White" Height="5"> <Path Stroke="Black" Data="M 0 3 l 8 0" /> </Grid> </VisualBrush.Visual> <VisualBrush.RelativeTransform> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="45" /> </VisualBrush.RelativeTransform> </VisualBrush>
尝试旋转水平线刷以避免角落间隙:
<VisualBrush TileMode="Tile" Viewport="0,0,5,5" ViewportUnits="Absolute" Viewbox="0,0,5,5" ViewboxUnits="Absolute"> <VisualBrush.Visual> <Grid Background="White" Height="5"> <Path Stroke="Black" Data="M 0 3 l 8 0" /> </Grid> </VisualBrush.Visual> <VisualBrush.RelativeTransform> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="45" /> </VisualBrush.RelativeTransform> </VisualBrush>
回答by Jamie Nordmeyer
I finally got it working, but not with a single composite brush like I was wanting. I used the following styles:
我终于让它工作了,但没有像我想要的那样使用单个复合画笔。我使用了以下样式:
<Style x:Key="WindowBackground" TargetType="Grid">
<Setter Property="Background">
<Setter.Value>
<RadialGradientBrush GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="#EAA659" Offset="0"/>
<GradientStop Color="#BF8230" Offset="1"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HatchOverlay" TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush Opacity=".1" TileMode="Tile" Viewport="0,0,10,20" ViewportUnits="Absolute">
<VisualBrush.Visual>
<Canvas>
<Path Stroke="#825821" Data="M 0 0 l 10 20" />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
Then, to enable it at run time, I added a rectangle as the first child of my grid, like so:
然后,为了在运行时启用它,我添加了一个矩形作为网格的第一个子元素,如下所示:
<Grid Style="{StaticResource WindowBackground}">
<Rectangle Style="{StaticResource HatchOverlay}"/>...
This gave me the effect that I was after. Thanks for your help; I'll up-vote each of you.
这给了我想要的效果。谢谢你的帮助; 我会给你们每个人投票。

