WPF图画在帆布盘区背景的色的网格线

时间:2013-12-23 17:51:56

标签: c# wpf canvas background

与方格纸的外观类似,我想在画布的背景上绘制不同颜色的直线以形成网格。以下代码适用于仅绘制红线。我还想绘制一些蓝线和灰线。这意味着我还需要两组线,到目前为止,我还没能解决绘制新的其他颜色线的问题。

<Window x:Class="GridTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="700" Width="1000">

<ScrollViewer HorizontalScrollBarVisibility="Visible">
    <Canvas Width="10000" Height="10000">
        <Canvas.Background>
            <DrawingBrush Stretch="None" TileMode="Tile"
                          Viewport="0,0 100,100" ViewportUnits="Absolute">
                <DrawingBrush.Drawing>
                    <GeometryDrawing>
                        <GeometryDrawing.Geometry>
                            <GeometryGroup>
                                <LineGeometry StartPoint="0,0" EndPoint="0,100"/>
                                <LineGeometry StartPoint="0,0" EndPoint="100,0"/>
                            </GeometryGroup>
                        </GeometryDrawing.Geometry>
                        <GeometryDrawing.Pen>
                            <Pen Thickness="1" Brush="Red"/>
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Canvas.Background>
    </Canvas>
</ScrollViewer>

</Window>

重申......这段代码正在做我想要的单色。但我也想添加不同颜色的线条。

1 个答案:

答案 0 :(得分:6)

您可以在根画布上添加多个画布,每个画布都有自己的背景笔:

<ScrollViewer HorizontalScrollBarVisibility="Visible">
    <Canvas Width="10000" Height="10000">
        <Canvas  Width="10000" Height="10000">
            <Canvas.Background>
                <DrawingBrush Stretch="None" TileMode="Tile"
                          Viewport="0,0 10,10" ViewportUnits="Absolute">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <LineGeometry StartPoint="0,0" EndPoint="0,10"/>
                                    <LineGeometry StartPoint="0,0" EndPoint="10,0"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Pen>
                                <Pen Thickness="1" Brush="DarkGray"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>
        <Canvas  Width="10000" Height="10000">
            <Canvas.Background>
                <DrawingBrush Stretch="None" TileMode="Tile"
                          Viewport="0,0 50,50" ViewportUnits="Absolute">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <LineGeometry StartPoint="0,0" EndPoint="0,50"/>
                                    <LineGeometry StartPoint="0,0" EndPoint="50,0"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Pen>
                                <Pen Thickness="1" Brush="Blue"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>
        <Canvas  Width="10000" Height="10000">
            <Canvas.Background>
                <DrawingBrush Stretch="None" TileMode="Tile"
                          Viewport="0,0 100,100" ViewportUnits="Absolute">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <LineGeometry StartPoint="0,0" EndPoint="0,100"/>
                                    <LineGeometry StartPoint="0,0" EndPoint="100,0"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Pen>
                                <Pen Thickness="1" Brush="Red"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>
    </Canvas>
</ScrollViewer>

或者,您可以使用VisualBrush,它构建任何UIElement的画笔。因此,您可以使用Grid内置Rectangles或任何类似的方法制作画布背景:

<Canvas> 
    <Canvas.Background>
        <VisualBrush>
            <VisualBrush.Visual>
                <Grid>
                    <Rectangle Width="10000" Height="10000">
                        <Rectangle.Fill>
                            <DrawingBrush Stretch="None" TileMode="Tile"
                                Viewport="0,0 10,10" ViewportUnits="Absolute">
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <LineGeometry StartPoint="0,0" EndPoint="0,10"/>
                                                <LineGeometry StartPoint="0,0" EndPoint="10,0"/>
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                        <GeometryDrawing.Pen>
                                            <Pen Thickness="1" Brush="DarkGray"/>
                                        </GeometryDrawing.Pen>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Width="10000" Height="10000">
                        <Rectangle.Fill>
                            <DrawingBrush Stretch="None" TileMode="Tile"
                                Viewport="0,0 50,50" ViewportUnits="Absolute">
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <LineGeometry StartPoint="0,0" EndPoint="0,50"/>
                                                <LineGeometry StartPoint="0,0" EndPoint="50,0"/>
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                        <GeometryDrawing.Pen>
                                            <Pen Thickness="1" Brush="Blue"/>
                                        </GeometryDrawing.Pen>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Width="10000" Height="10000">
                        <Rectangle.Fill>
                            <DrawingBrush Stretch="None" TileMode="Tile"
                                Viewport="0,0 100,100" ViewportUnits="Absolute">
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <LineGeometry StartPoint="0,0" EndPoint="0,100"/>
                                                <LineGeometry StartPoint="0,0" EndPoint="100,0"/>
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                        <GeometryDrawing.Pen>
                                            <Pen Thickness="1" Brush="Red"/>
                                        </GeometryDrawing.Pen>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Grid>
            </VisualBrush.Visual>
        </VisualBrush>
    </Canvas.Background>
</Canvas>