2010-03-15 143 views

回答

34

您可以简单地使用Window.LeftWindow.Top属性。从您的主窗口读取它们,并在调用ShowDialog()方法之前将值(加上20像素或其他)分配给AboutBox

AboutBox dialog = new AboutBox(); 
dialog.Top = mainWindow.Top + 20; 

有它的中心,你也可以简单地使用WindowStartupLocation属性。如果你希望它是水平居中,而不是垂直方向设置这WindowStartupLocation.CenterOwner

AboutBox dialog = new AboutBox(); 
dialog.Owner = Application.Current.MainWindow; // We must also set the owner for this to work. 
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner; 

(即固定的垂直位置),你必须做的是,在事件处理程序的AboutBox的加载后,因为您需要根据AboutBox的宽度计算水平位置,并且只有在加载后才知道。

protected override void OnInitialized(...) 
{ 
    this.Left = this.Owner.Left + (this.Owner.Width - this.ActualWidth)/2; 
    this.Top = this.Owner.Top + 20; 
} 

gehho。

+0

谢谢gehho。 – empo 2010-03-15 15:17:35

+0

这将工作DataGridCell(在DataGrid wpf4内))吗?显然它不是。 – neebz 2011-02-23 20:34:20

+0

@nEEbz:你什么意思?你想移动一个'DataGridCell'相对于主窗口?我不明白与原始问题的关系。请详细说明。 – gehho 2011-03-03 13:02:24

2

我会去手工的方式,而不是在WPF数进行计算的我..

System.Windows.Point positionFromScreen = this.ABC.PointToScreen(new System.Windows.Point(0, 0)); 
PresentationSource source = PresentationSource.FromVisual(this); 
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(positionFromScreen); 

AboutBox.Top = targetPoints.Y - this.ABC.ActualHeight + 15; 
AboutBox.Left = targetPoints.X - 55; 

哪里ABC是父窗口内的一些的UIElement(可能是老板,如果你喜欢..) ,也可以是窗口本身(左上角点)..

祝你好运

+1

伟大的解决方案! – VibeeshanRC 2016-06-27 10:18:48