我尝试并排绘制一些矩形,但矩形之间可能存在一个像素。在我设置矩形的 SnapsToDevicePixels = true 或 UseLayoutRounding = true 之后会更好,但是在我调整窗口大小之后,仍然会出现间隙。
这是有关绘制矩形的代码。
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
DrawRectangle();
}
private void DrawRectangle()
{
var width = this.grid1.ActualWidth;
var height = this.grid1.ActualHeight;
var step = width / 15;
this.grid1.Children.Clear();
for (int i = 0; i < 15; i++)
{
Rectangle rectangle = new Rectangle();
rectangle.Width = step;
rectangle.Height = height;
rectangle.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
rectangle.Margin = new Thickness(i * step, 0, 0, 0);
rectangle.Fill = new SolidColorBrush(Colors.Blue);
rectangle.SnapsToDevicePixels = true;
//rectangle.UseLayoutRounding = true;
this.grid1.Children.Add(rectangle);
}
}