2013-03-19 119 views
3

參考this question我嘗試使用網格線作爲我的用戶控件。在設計時控制看起來不錯,但是當我嘗試到組件插入到我的主窗口中的所有網格線走了,取而代之的是醜陋的灰色角落:WPF畫布中的網格線

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

的這個結果:User control preview

,這是它看起來像我的主窗口:Main window

你有何嵌入用戶控件到我的窗口時,我的背景瓷磚不能正常工作任何想法?

回答

9

試試這個:

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

這不會正常工作在某些情況下 – GorillaApe 2014-09-26 22:00:50

+1

@Parhs哪些案件? – 2014-09-26 23:23:57

+0

我的意思是如果你有一個具有縮放功能的繪圖應用程序。由於舍入誤差,網格線將被錯誤定位。我的意思是像autocad與自適應網格大小不是一個簡單的規模變換 – GorillaApe 2014-09-27 08:43:06