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?
When a window is shown, it is measured, then WindowStartupLocation
is processed using the ActualWidth
and ActualHeight
of the window computed by the measure process.
The behavior you describe tells me ActualWidth
and ActualHeight
are measured to be either zero or relatively small at the time of the Show() or ShowDialog() call and only later set to nonzero values.
This can happen if, for example, the content of the window is built using a DataContext that is only set on a Loaded
event. When the Show()
is called, the window has not been Loaded
yet so it has no data. Later when the Loaded
event fires it sets DataContext and the window updates its content but the positioning has already occured.
There are many other scenarios, for example contents filled using a Dispatcher.BeginInvoke call, or from a separate thread, or bindings that are delayed or asynchronous.
Basically you need to look for anything that could cause the content of your window to be smaller than normal at the moment Show()
is called, and fix it.