WPF画布中的网格线

5

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

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

这是结果: 用户控件预览

这是在我的主窗口中的效果: 主窗口

请问为什么当我把用户控件嵌入到我的窗口时,背景图案无法正常工作?

1个回答

15

试一下这个:

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

我的意思是,如果您有一个具有缩放功能的绘图应用程序。由于舍入误差,网格线会被错误地定位。我指的是像AutoCAD一样具有自适应网格大小而不是简单的比例变换。 - GorillaApe
在我的缩放环境中,网格线工作完美。 - AgentFire

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接