如何在WPF中创建重复的对角线作为背景?

时间:2013-02-02 19:35:58

标签: wpf

好吧,我已经破坏了我的大脑,谷歌和stackoverflow试图解决这个问题,但只是无法得到它。我正在尝试使用DrawingBrush和DrawingGroup对我的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>

3 个答案:

答案 0 :(得分:19)

这个技巧应该有用。

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

答案 1 :(得分:1)

我终于让它工作了,但不是像我想要的那样只用一个复合刷。我使用了以下样式:

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

然后,为了在运行时启用它,我添加了一个矩形作为我的网格的第一个子节点,如下所示:

<Grid Style="{StaticResource WindowBackground}">
    <Rectangle Style="{StaticResource HatchOverlay}"/>...

这让我觉得我追求的效果。谢谢你的帮助;我会向你们每个人投票。

答案 2 :(得分:1)

  • 尝试旋转水平线刷以避免角落间隙:

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