tags:

views:

66

answers:

1

hi guys,

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. reason for using this is i need to remove the elements from objects canvas and redraw it again, but need to keep the grid lines without removing them. so i remove the children from object canvas and redraw it time to time. if i use the same canvas when i remove the objects the gridlines also disapere. guys 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.

+2  A: 

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;
        }
    }
}
mdm20