2015-09-28 75 views
1

什么时候WPF需要this.WindowStartupLocation = WindowStartupLocation.CenterOwner;效果?什么时候WindowStartupLocation生效

我想这是默认行为,但如果存在一些窗口布局信息(上,左,...),我想设置它们在窗口的Loaded事件中。但是这不能正常工作。

如果我评论出WindowStartupLocation,它的工作原理。

也许有人解决过这个问题。谢谢!

回答

1

WindowStartupLocation仅在启动时生效并将窗口设置在中心,因此一旦设置了窗口,就不能对调整窗口大小或移动窗口产生任何其他影响。

MSDN说:

获取或设置当第一显示的窗口的位置。

也看到这个MSDN blog它表明你如何挂钩它作为默认行为:

private void SizeChangedHandler(Object sender, SizeChangedEventArgs e) 
{ 
    Rect workArea = SystemParameters.WorkArea; 
    this.Left = (workArea.Width - this.ActualWidth)/2; 
    this.Top = (workArea.Height - this.ActualHeight)/2; 
} 

private void LocationChangedHandler(Object sender, EventArgs e) 
{ 
    Rect workArea = SystemParameters.WorkArea; 
    this.Left = (workArea.Width - this.ActualWidth)/2; 
    this.Top = (workArea.Height - this.ActualHeight)/2; 
} 
+0

没错,但首先显示也可以是Loaded事件后,还是我错了? – BendEg