How to cope with the final events of a window and UI dimensions

Viewed 48

I have searched and googled a lot about that but still am not clear about how things work.


INTRODUCTION: where the problem comes from.

I am currently working on a program in which there is a grid the draws some elements that are run-time defined. The window can be resized/maximed and so the grid, so when I draw elements inside I have to know the grid dimensions. Element data are serialized so when the program restarts all data is loaded and grid has to be accordingly drawn. Datagrid definition is:

 <Grid Name="grdPalletTab2" AllowDrop="True" Drop="Grid_Drop" Effect="{x:Null}" PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

HYPOTHESIS: what I hope is correct. If not please let me know

When a UI Element is defined omitting width and height, those values shall be Nan. But after the element is fully rendered ActualWidht and ActualHeight shall have a value.


PROBLEM: where the problem comes from.

At the window loaded event the grid has ActualWidth and ActualHeight are zero.


SEARCHING AND NOT FINDING A SOLUTION:

I have found on a lot of sites that the last init event for a window is Content rendered so I have analyzed the grid on it. But the grid keeps on stubbornly having zero ActualWidth and ActualHeight.


TO MAKE THINGS WORSE:

I have followed this nice article

http://www.codeproject.com/Articles/403418/Lifetime-events-of-a-WPF-application

where it says:

Loaded: (Window)

Your entire window is ready. Last stop before rendering. All animation, styles and databinding will be ready here. Any visual tweaks needs to be done here.

This works in reverse order when compared with Initialized event. It is raised from root to child element from the fully constructed logical element tree. So window Load event will be raised first followed by any child element events.

ContentRendered: (Window)

As the name implies it occurs after content was rendered. If there is no content this event will not be fired at all. Don't do any visual tweaks here. From here you may set a flag for your business logic that window is up, running and shown to the user for the first time.

What I am not clear about here is why any visual tweaks needs to be done here and not after the window is rendered. I mean I can understand that if you render and then change this can be not visually nice and can lead to additional delays but it it the only reason? And if I need to know UI dimensions I can only know their actual dimension after they are drawn.

ADD please notice that if I read the ActualWidth and ActualHeight read on a later time (e.g. on a Button_Click) they have real and not zero value.

0 Answers
Related