wpf WPF画布中的网格线

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

Grid line in WPF canvas

c#wpfxamlcanvas

提问by Marc Dannemann

Referring to this questionI tried to use grid lines for my user control. At design time the control looks good, but when I try to insert the component into my main window all the grid lines are gone away and instead there is that ugly gray corner:

参考这个问题,我尝试为我的用户控件使用网格线。在设计时控件看起来不错,但是当我尝试将组件插入主窗口时,所有网格线都消失了,取而代之的是丑陋的灰色角落:

<UserControl x:Class="mx_sachdaten.View.FormWorkspaceView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Loaded="FormWorkspaceView_Loaded"
         d:DesignHeight="400" d:DesignWidth="400">
    <UserControl.Resources>
        <DrawingBrush x:Key="FormCanvasGridTile" Stretch="None" TileMode="Tile" 
                  Viewport="0,0,30,30" ViewportUnits="Absolute">
            <DrawingBrush.Drawing>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <LineGeometry StartPoint="0,0" EndPoint="30,0" />
                            <LineGeometry StartPoint="30,0" EndPoint="30,30" />
                            <LineGeometry StartPoint="30,30" EndPoint="0,30" />
                            <LineGeometry StartPoint="0,30" EndPoint="0,0" />
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Pen>
                        <Pen Thickness="1" Brush="LightGray" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Brush>White</GeometryDrawing.Brush>
                </GeometryDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Canvas Grid.Column="0" x:Name="FormCanvas" Background="{StaticResource FormCanvasGridTile}"
            MouseLeftButtonDown="FormCanvas_MouseLeftButtonDown" VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch" />
        <ToolBarTray Grid.Column="1" Orientation="Vertical">
            <ToolBar Band="1">
                <Button Content="Label" HorizontalAlignment="Right" />
                <Button Content="TextBox" HorizontalAlignment="Right" />
                <Button Content="Button" HorizontalAlignment="Right" />
            </ToolBar>
        </ToolBarTray>
    </Grid>
</UserControl>

Result of this: User control preview

结果:用户控件预览

And this is how it looks like on my main window: Main window

这就是我的主窗口上的样子:主窗口

Do you have any idea why my background tile isn't working properly when embedding the user control into my window?

在将用户控件嵌入到我的窗口中时,您知道为什么我的背景图块无法正常工作吗?

回答by Federico Berasategui

Try this:

尝试这个:

<Border>
    <Border.Background>
        <VisualBrush TileMode="Tile"
            Viewport="0,0,50,50" ViewportUnits="Absolute" 
            Viewbox="0,0,50,50" ViewboxUnits="Absolute">
            <VisualBrush.Visual>
                <Rectangle Stroke="Darkgray" StrokeThickness="1" Height="50" Width="50"
                StrokeDashArray="5 3"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Border.Background>
</Border>