2010-03-15 36 views

Respuesta

34

Puede simplemente usar las propiedades Window.Left y Window.Top. Léelos desde su ventana principal y asigne los valores (más 20 px o lo que sea) al AboutBox antes de llamando al método ShowDialog().

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

tenerlo centrado, también puede simplemente utilizar la propiedad WindowStartupLocation. Ponga esto en 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; 

Si desea que se centra horizontalmente, pero no verticalmente (es decir, fija la ubicación vertical), que tendrá que hacer eso en un manejador de sucesos después de la AboutBox se ha cargado, ya que necesitará para calcule la posición horizontal según el Ancho del AboutBox, y esto solo se conoce después de haber sido cargado.

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

gehho.

+0

Gracias gehho. – empo

+0

¿funcionará esto para DataGridCell (dentro de DataGrid wpf4) también? aparentemente no es así – neebz

+0

@nEEbz: ¿Qué quieres decir? ¿Desea mover un 'DataGridCell' relativo a la ventana principal? No entiendo la relación con la pregunta original. Por favor elabora. – gehho

2

me gustaría ir la forma manual, en lugar de recuento en WPF para hacer el cálculo para mí ..

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; 

Dónde ABC es un poco de UIElement dentro de la ventana padre (podría ser el propietario si te gusta ..) , Y también podría ser la ventana en sí (punto superior izquierdo) ..

Buena suerte

+1

¡¡Gran solución !!! – VibeeshanRC

Cuestiones relacionadas