tags:

views:

1238

answers:

2

I need to implement a canvas which scales it's contents according to it's size. I know there is Viewbox, which scales everything inside of it. However I cannot use that, because some elements have a fixed size and cannot be scaled.

Also how can I bind the size of the Canvas to the parent element (for example a resizable window). There is sizeToContent for windows, I want the size fitting exactly the other way round. Also the canvas uses some drawing based on the size of the hosting element, how is redraw triggered and how can I ensure that it only draws if it gets a valid (or min) size?

+2  A: 

If you don't specify any width or height to the canvas it automatically uses all the available space. This is because the default VerticalAlignment and HorizontalAlignment are set to Stretch.

What do you mean by canvas that scales it's contents according to it's size without scaling all the contents as some have fixed size?

Update after comments

If your drawing algorithm already scales the content to the canvas' height and width then all you need to do is to resize the canvas to fit the area I believe? In that case just remove the hardcoded height/width values and the canvas will resize to fit the container.

You might need to use ActualHeight/ActualWidth instead of Height/Width in the drawing algorithm after this though. ActualHeight/ActualWidth return the values that the layout container will give your canvas so these represents the values the canvas is drawn with.

Mikko Rantanen
Well there is some drawing in the canvas, the drawing algo uses the height and with property of the canvas as a constant and things are drawn (or stretched) relative to this
Nils
If I set everything to stretch, Height and With of the canvas are not defined.. I guess I need to catch this in the code?
Nils
If I set everything to Strech, Height and With of the canvas are not defined when drawing first.. guess I need to catch this in the code
Nils
I updated the answer to reflect this. You can use ActualHeight and ActualWidth properties to find out the REAL height and width of the canvas.
Mikko Rantanen
I think ActualHeight/ActualWidth is what I need, however when drawing it's first set to 0, instead of Height/Width which are set to NaN.I also need to to redraw everything once ActualHeight/-Width changes, how can I do that?
Nils
@Nils, there's a SizeChanged event you could react to.
Mikko Rantanen
aha, I was trying to attach it to CompositionTarget.Rendering
Nils
+2  A: 

I think you can find the answers to all your questions in my London Underground demo.

HTH, Kent

Kent Boogaart
thx will have a look at it later
Nils