Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF : How to set a Dialog position to show at the center of the application?

Tags:

dialog

wpf

How to set Dialog's position that came from .ShowDialog(); to show at the center of the mainWindows.

This is the way I try to set position.

private void Window_Loaded(object sender, RoutedEventArgs e) {             PresentationSource source = PresentationSource.FromVisual(this);     if (source != null)     {         Left = ??         Top = ??     } } 
like image 642
Prince OfThief Avatar asked Nov 29 '10 18:11

Prince OfThief


People also ask

How do I center a WPF window?

Centring Windows To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed. Run the program and click the button to see the result.


2 Answers

In the XAML belonging to the Dialog:

<Window ... WindowStartupLocation="CenterOwner"> 

and in C# when you instantiate the Dialog:

MyDlg dlg = new MyDlg(); dlg.Owner = this;  if (dlg.ShowDialog() == true) {     ... 
like image 74
chessweb Avatar answered Oct 05 '22 22:10

chessweb


I think it's easier to use xaml markup

<Window WindowStartupLocation="CenterOwner"> 
like image 20
The Smallest Avatar answered Oct 05 '22 23:10

The Smallest