WPF set parent (Window)

2019-06-19 22:14发布

问题:

How do you set the parent of a WPF window when you want to show it as a dialog? Both methods Show() and ShowDialog() don't seem to have that option.

This was possible in Java as you could pass the parent in the constructor. Is it possible in any way in WPF?

EDIT: I'm using C#

回答1:

owner can be set but parent is a readonly property.

var w = new Window();
w.Owner = Window.GetWindow(this);
w.Show();


回答2:

on your "Showdialog" object do:

templateWindow.Owner= System.Windows.Application.Current.MainWindow;
templateWindow.ShowDialog();


标签: c# wpf window