我有一个自定义的 WPF Canvas ,我想在其上显示一个网格。我通过覆盖 Canvas 上的 OnRender 方法并使用 DrawingContext 绘图函数来实现。 IsGridVisible、GridWidth、GridHeight 分别是每条网格线水平和垂直之间的像素数。
我还在 Canvas.LayoutTransform 属性上使用 ScaleTransform 来缩放 Canvas 项目,正如预期的那样,网格线粗细乘以 ScaleTransform 缩放因子,如下图所示。有没有办法绘制单个像素线,而不管当前的 Canvas RenderTransform 是什么?
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc);
if (IsGridVisible)
{
// Draw GridLines
Pen pen = new Pen(new SolidColorBrush(GridColour), 1);
pen.DashStyle = DashStyles.Dash;
for (double x = 0; x < this.ActualWidth; x += this.GridWidth)
{
dc.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight));
}
for (double y = 0; y < this.ActualHeight; y += this.GridHeight)
{
dc.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
}
}
}
alt text http://www.freeimagehosting.net/uploads/f05ad1f602.png
最佳答案
作为对原始帖子状态的评论。笔粗细应设置为 1.0/缩放。
关于WPF Canvas - 单像素网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916496/