How to get WPF element sizes at runtime without specifying them at compile time

Problem?

<UI:PanelBrowser Margin="12,27,12,32"></UI:PanelBrowser>

WPF is ridiculous in that non-manually defined properties (such as width and height) in this case make them have values Doulbe.NaN. The problem is that I need to know this number. I will not manually set the width and height in XAML, because it does not allow resizing.

Given the above XAML snippet (this object is a simple subclass of the border control), how can I get the values ​​of the Width and Height properties at runtime?

Edit:

Wow, I feel ridiculous. I read about ActualWidthand ActualHeight, but they successively returned 0 and 0 for me. The reason is that I tested these properties in the constructor of the Framework element before they were actually initialized. Hope this helps someone who is facing the same issue and experiencing errors. :)

+3
source share
3 answers
+10
source

You can use the ' ActualWidthand properties ActualHeightto get width and height values ​​when drawing.

+1
source

The WPE FrameworkElement class provides two DependencyProperties functions for this: FrameworkElement.ActualWidthand FrameworkElement.ActualHeightget the rendered width and height at runtime.

+1
source

Source: https://habr.com/ru/post/1763914/


All Articles