WPF - Set dialog window position relative to main window?

后端 未结 2 2027
情深已故
情深已故 2021-01-07 19:26

I\'m just creating my own AboutBox and I\'m calling it using Window.ShowDialog()

How do I get it to position relative to the main window, i.e. 20px from the top and

2条回答
  •  时光取名叫无心
    2021-01-07 19:41

    I would go the manual way, instead of count on WPF to make the calculation for me..

    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;
    

    Where ABC is some UIElement within the parent window (could be Owner if you like..) , And could also be the window itself (top left point)..

    Good luck

提交回复
热议问题