Search code examples
c#canvaswindows-phone

How do I get the width and height of a Canvas element that is set to 'Auto'?


I'm developing an App for Windows Phone 8.1, and I have a canvas that draws a ball onto it programmatically. My problem is I want to set the boundaries of the canvas to be that of its width and height, but I'm using grids with relative sizes for the size of the canvas itself.

I'm wondering how I can get the width and height of the canvas - I've tried actualwidth and actualheight but they're just showing '0'.


Solution

  • You can put the Canvas inside something (a Border, a Grid), and then get the ActualWidth and ActualHeight from the container.

    I have the impression that Canvas does not expand to fill container, so it is better to get dimensions from the container itself. Just be sure the container layout is the same you would want your Canvas to have.