Question

i created a class "Component", which will be added to the children of my canvas. After the adding, i want to position these components using for example:

comp.setValue(Canvas.LeftProperty, 100d);

which works fine. After that the component will be added to canvas. Now my actual problem is to get the components actual width. I tried these things like

comp.Width;
comp.ActualWidth;

but they are 0 or NaN. In my understanding, this should be like that. Cause they have been added to the canvas, and should than obtain there specific sizes, or am i wrong with this?

Also, i added an OnClick-Event on those components, in which they write their ActualWidth on the console, and it works. But i want to get these informations directly after adding them..

Any ideas?

Greetings and stuff!

Was it helpful?

Solution

The framework will trigger a measure and arrange cycle when you add children to a panel that is included in the visual tree. After this layout pass, you can get the size by asking ActualWidth and ActualHeight. Apparently the cycle is not completed (or yet to be triggered) when your code runs. So what can you do? You can register an event handler on FrameworkElement.SizeChanged event and run your code inside the event handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top