views:

570

answers:

1

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 centered.

Thanks.

A: 

You can simply use the Window.Left and Window.Top properties. Read them from your main window and assign the values (plus 20 px or whatever) to the AboutBox before calling the ShowDialog() method.

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

To have it centered, you can also simply use the WindowStartupLocation property. Set this to WindowStartupLocation.CenterOwner

AboutBox dialog = new AboutBox();
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

If you want it to be centered horizontally, but not vertically (i.e. fixed vertical location), you will have to do that in an EventHandler after the AboutBox has been loaded because you will need to calculate the horizontal position depending on the Width of the AboutBox, and this is only known after it has been loaded.

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

gehho.

gehho
Thank you gehho.
empo