ventanas ventana navegacion entre abrir wpf window position relative

navegacion - abrir ventana wpf c#



WPF-¿Establecer la posición de la ventana de diálogo relativa a la ventana principal? (2)

Me gustaría ir en forma manual, en lugar de contar con WPF para hacer el cálculo por 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;

Donde ABC es un UIElement dentro de la ventana principal (podría ser Propietario si lo desea ...), Y también podría ser la ventana en sí (punto superior izquierdo).

Buena suerte

Solo estoy creando mi propio AboutBox y lo llamo usando Window.ShowDialog ()

¿Cómo puedo colocarlo en una posición relativa a la ventana principal, es decir, 20 px desde la parte superior y centrado?

Gracias.


Simplemente puede usar las propiedades Window.Left y Window.Top . Léalos desde su ventana principal y asigne los valores (más 20 px o lo que sea) a AboutBox antes de llamar al método ShowDialog() .

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

Para centrarlo, también puede usar la propiedad WindowStartupLocation . Establezca 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 esté centrado horizontalmente, pero no verticalmente (es decir, en una ubicación vertical fija), tendrá que hacerlo en un EventHandler después de que se haya cargado el AboutBox porque necesitará calcular la posición horizontal dependiendo del Ancho del AboutBox , y esto solo se conoce después de que ha sido cargado.

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

gehho