Wpf 画布上的绘图刷不工作
我在问题中的XAML是直接从问题中获取的。只是XAML创建了一个平铺画笔,在画布的背景下渲染矩形(在我的例子中是正方形)。我的问题是,只有当我将画布的宽度和高度设置为“自动”以外的任何值时,这个XAML才起作用。但当我将画布的宽度和高度设置为“自动”时,背景上就不会绘制任何正方形 如何将我的宽度和高度设置为“自动”,并使用DrawingBrush在画布上渲染正方形? 这是我的XAML:Wpf 画布上的绘图刷不工作,wpf,xaml,Wpf,Xaml,我在问题中的XAML是直接从问题中获取的。只是XAML创建了一个平铺画笔,在画布的背景下渲染矩形(在我的例子中是正方形)。我的问题是,只有当我将画布的宽度和高度设置为“自动”以外的任何值时,这个XAML才起作用。但当我将画布的宽度和高度设置为“自动”时,背景上就不会绘制任何正方形 如何将我的宽度和高度设置为“自动”,并使用DrawingBrush在画布上渲染正方形? 这是我的XAML: <Window.Resources> <DrawingBrush x:Key="Grid
<Window.Resources>
<DrawingBrush x:Key="GridTile" Stretch="None" TileMode="Tile"
Viewport="0,0 20,20" ViewportUnits="Absolute">
<!-- ^^^^^^^^^^^ set the size of the tile-->
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<!-- draw a single X -->
<GeometryGroup>
<!-- top-left to top-right -->
<LineGeometry StartPoint="0,0" EndPoint="20,0" />
<!-- top-left to bottom-left -->
<LineGeometry StartPoint="0,0" EndPoint="0,20" />
<!-- bottom-left to bottom-right -->
<LineGeometry StartPoint="0,20" EndPoint="20,20" />
<!-- top-right to bottom-right -->
<LineGeometry StartPoint="20,0" EndPoint="20,20" />
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<!-- set color and thickness of lines -->
<Pen Thickness="1" Brush="Silver" />
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="OffsetGrid" Stretch="None" AlignmentX="Left" AlignmentY="Top">
<DrawingBrush.Transform>
<!-- set the left and top offsets -->
<TranslateTransform X="0" Y="0" />
</DrawingBrush.Transform>
<DrawingBrush.Drawing>
<GeometryDrawing Brush="{StaticResource GridTile}" >
<GeometryDrawing.Geometry>
<!-- set the width and height filled with the tile from the origin -->
<RectangleGeometry Rect="0,0 160,160" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Window.Resources>
<Canvas Name="canvasMain" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto" Background="{StaticResource OffsetGrid}"></Canvas>
尝试更改路线:
<Canvas Name="canvasMain" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Background="{StaticResource OffsetGrid}"></Canvas>