Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf center child window not working with sizetocontent

If i set SizeToContent to WidthAndHeight, then WindowStartupLocation="CenterOwner" does not work properly. Instead of the center of the new window to be at the center of its parent owner, it looks more like the top left hand corner of the child window to be at the center of the parent. If i remove SizeToContent then all is ok. What is wrong?

like image 660
immuner Avatar asked Nov 16 '09 00:11

immuner


1 Answers

Binded dynamic content is mostly rendered Gui-directly but sometimes GUI-dispatched. Timer and other threads can initiate (MVVM) property change events. It is sure, that the rendering is done in near time, but not garanted, because position an priority of the WPF Dispacher queue. So, you can not say when rendering is finished, and WPF cannot say something about the order of processing- so WPF cannot now the ideal time to calculate the StartPosition.

A trick is, to wait, that the WPF-queue is emtpy. Then you are sure that, WPF has time to process your code. That means, you delay the ShowDialog call for the Window.

So give the GUI-Main Thread all the time it needs, to perform the dynamic content changes for MVVM or other dynamics changes. Do not try to calculate the position manually, it is very complex, to support multi displays. Try this code to open the window, it opens the Window only, when WPF finished all operations.

        win.Dispatcher.Invoke(new Action(() => win.ShowDialog()), DispatcherPriority.ApplicationIdle);
like image 112
Steffen Gehring Avatar answered Oct 12 '22 10:10

Steffen Gehring