I have Image control in my Window. The size of this Image control is set to "Auto".
<Image x:Name="videoImg" Stretch="Fill" Height="Auto" Width="Auto" />
When i try to get access, it returns 0. How to see real size of this control? It resizes with window.
You can use .ActualHeight
and .ActualWidth
to get the rendered Height
/Width
of a control
The thing is, the Width
and Height
properties let you express the desired size, whereas what you want is the rendered size - which can be accessed (but not set) using the ActualWidth
and ActualHeight
properties.
It should be noted that these aren't static values either, that is, once set they are not necessarily going to be the same forever after, as it will be re-evaluated upon each rendering sequence...
Because [ActualHeight / ActualWidth] is a calculated value, you should be aware that
there could be multiple or incremental reported changes to it as a
result of various operations by the layout system. The layout system
may be calculating required measure space for child elements,
constraints by the parent element, and so on.
So, depending on your requirements, you might want to consider re-evaluating your data at appropriate points, perhaps when the containing control resizes, for instance.
While some of WPF controls fill up all available space when laid out and rendered, the others don't.
Specifically, the Image control is not of a kind that establishes its size on its own, i.e., in cases when you do not specify control's size explicitly with width/height attributes or the like.
But the Grid control fills up all available space when lacking size-defining attributes. The Page/Window template in Visual Studio has a Grid control as a child of a Page/Window root control, and when the user starts to put controls on a page in a graphical editor, the user-added controls first become children of this Grid control.
If you have used the VS template, and your Image control is a child of the said Grid control, name your Grid with an x:Name attribute, and you can use the Grid's ActualWidth/Height properties for your needs in a code-behind, because the image control grows up to its parent Grid size -- provided you do not specify its size explicitly or otherwise, i.e., setting the Image content.
By the way, the sizing behavior of built-in controls can be changed. You can modify a control and override corresponding dependency properties. See, for example, https://stackoverflow.com/a/6094993 .