I'm developing a graph control in WPF. I need to place a Canvas
on top of another. One Canvas
contains the grid lines and the other Canvas
draws the objects on it.
The reason for doing this is I need to remove the elements from object's canvas and redraw it again, but need to keep the grid lines without removing them.
So I remove the children from object's canvas 开发者_JAVA百科and redraw it time to time. If I use the same canvas when I remove the objects the gridlines also disapere.
Is there any way to place one canvas on top of another? Or is there any other solution for my problem?
please help.
regards, rangana.
There are a few ways you could do this. Here is one. Just extend the Canvas class and draw the grid yourself in the OnRender method.
public class GridCanvas : Canvas
{
public int rows = 4;
public int cols = 4;
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
double yStep = this.ActualHeight / rows;
double y = yStep;
for (int i = 0; i < rows - 1; i++)
{
dc.DrawLine(new Pen(Brushes.Black, 1), new Point(0, y), new Point(this.ActualWidth, y));
y += yStep;
}
double xStep = this.ActualWidth / cols;
double x = xStep;
for (int i = 0; i < cols - 1; i++)
{
dc.DrawLine(new Pen(Brushes.Black, 1), new Point(x, 0), new Point(x, this.ActualHeight));
x += xStep;
}
}
}
精彩评论