views:

1028

answers:

2

I have a grid which represents some data, and i need a canvas to overlay on top of it to layout some lines. The canvas is inside it's own usercontrol

The problem is that the canvas and it's contents should autoresize when the grid resizes width and height.

I added the canvas inside a viewbox, but it didn't do the trick. When the grid resizes, the canvas doesn't. The purpose of the canvas is to overlay a ruler-like functionality on top of the grid.

Looking forward for your solutions,

MeTTaxa

EDIT

I cannot use a style on the grid to replace the canvas, because the grid is showing different information than the canvas does. Think of it as chart, in which there are bar columns of different sizes (in my case the grid) and the days are lines in an overlay (just as a Gannt Chart)

My code:

    taxCanvas = new TimeAxis();
    Grid.SetRowSpan(taxCanvas, GRightMain.RowDefinitions.Count);
    Grid.SetColumnSpan(taxCanvas, GRightMain.ColumnDefinitions.Count);

    Grid.SetColumn(taxCanvas, 0);
    Grid.SetRow(taxCanvas, 0);


    Grid.SetZIndex(taxCanvas, -1);

    taxCanvas.Height = GRight.ActualHeight;
    taxCanvas.Width = GRight.ActualWidth;

    GRightMain.Children.Add(taxCanvas);

TimeAxis is my canvas usercontrol, GRightMain is a grid which holds both my canvas and the grid with the content (Gright) in the same row and column.

Hope this helps

A: 

You could use binding to "bind" the size of the canvas and the size of the grid to each other. So when the gird resizes the canvas will do too automaticly. Also you could use a converter for the binding of you have to calculate and offset like a distance from the border.

Binding in WPF is always a good idea so make sure you know the basics of how it works.

Anyway you could always do it in the code-Behind (C#/VB) in the Size-Event. Just determine the size of one control and set it to another. Like getting the actualHeight property of the Grid and set it to the height property of the canvas.

Maybe this will give you an idea: http://blogs.msdn.com/bencon/archive/2006/05/10/594886.aspx or http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-converters

Holli
A: 

What is it that your are trying to do? If you just want to add gridlines to the whole grid you could do it by setting styles like this. Alternatively you could use Adorners. They are there to decorate/adorn elements on a separate layer. The good thing about adorners is, that allthough they are on a separate layer they keep in sync (size, position, transformation) with the element they adorn.

bitbonk