如何在运行时重置 WPF 表单的高度并重新居中?
我尝试在其 Loaded 事件中重置表单的高度,但表单的位置不再位于屏幕中心。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var manualHeight = 0
+ this.MessageRow.ActualHeight
+ this.ButtonsRow.ActualHeight
;
this.Height = manualHeight;
//What to do to re-center the form?
}
如果您知道该怎么做,请帮忙。非常感谢!
南。
[编辑]
感谢 xandy
的帮助,我在这里重新记下了答案。
var screenHeight += System.Windows.SystemParameters.PrimaryScreenHeight;
window.Top = (screenHeight - manualHeight) / 2;
I try to reset the height of my form in its Loaded event but the position of the form is not centered screen anymore.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var manualHeight = 0
+ this.MessageRow.ActualHeight
+ this.ButtonsRow.ActualHeight
;
this.Height = manualHeight;
//What to do to re-center the form?
}
Please help if you know how to. Great thanks!
Nam.
[Edit]
I re-note the answer here thanks to xandy
help.
var screenHeight += System.Windows.SystemParameters.PrimaryScreenHeight;
window.Top = (screenHeight - manualHeight) / 2;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
手动设置窗口 y 位置为:
window.Top = (screenHeight - manualHeight) / 2;
您可以在此处获取屏幕高度。
manually set the window y position to:
window.Top = (screenHeight - manualHeight) / 2;
where you can have the screenHeight here.