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 its 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.
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
Best Answer
A canvas is definitely the wrong approach in my opinion.
I strongly recommend looking up Adorners. You could create a custom adorner to do exactly this.
An Adorner is basically a "non-interactive window" that sits on top of all UIElements. It allows you to do anything you'd like (create controls, draw things, etc...) that will appear on top of the control itself.
Picture a wooden coffee table with a piece of clear glass on top of it. If you draw on the clear glass, you still see through to the coffee table. The only difference would be that you can actually reach straight through clear glass on the coffee table and touch the wood itself.
I hate posting MSDN links...but...eh. In this case it would be a good start:
http://msdn.microsoft.com/en-us/library/ms743737.aspx
EDIT:
I threw something together quickly. Hopefully this helps?
USERCONTROL:
THE USERCONTROL CODE-BEHIND:
FINALLY THE ADORNER ITSELF:
If you would like me to elaborate more on the code itself let me know.
I confirmed that this will draw a grid over top of whatever is on your main page. You should still be able to interact with what's beneath.