WPF中心子窗口不适用于sizetocontent [英] wpf center child window not working with sizetocontent

查看:156
本文介绍了WPF中心子窗口不适用于sizetocontent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将SizeToContent设置为WidthAndHeight,则WindowStartupLocation="CenterOwner"无法正常工作.而不是新窗口的中心位于其父所有者的中心,它看起来更像子窗口的左上角位于父窗口的中心. 如果我删除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?

推荐答案

显示一个窗口时,对其进行测量,然后使用由测量过程计算出的窗口的ActualWidthActualHeight处理WindowStartupLocation

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.

您描述的行为告诉我ActualWidthActualHeight在Show()或ShowDialog()调用时被测量为零或相对较小,并且仅在以后设置为非零值.

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.

例如,如果使用仅在Loaded事件上设置的DataContext构建窗口的内容,则会发生这种情况.调用Show()时,该窗口尚未成为Loaded,因此它没有数据.稍后,当Loaded事件触发时,它会设置DataContext,并且窗口会更新其内容,但是定位已经发生.

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.

还有许多其他情况,例如,使用Dispatcher.BeginInvoke调用填充内容,或者使用单独的线程填充内容,或者使用延迟或异步绑定.

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.

基本上,您需要查找在调用Show()时可能导致窗口内容小于正常内容的所有内容,并进行修复.

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.

这篇关于WPF中心子窗口不适用于sizetocontent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆