如何通过XAML将Window的所有者设置为Application.Current.MainWindow
?到目前为止,我已经尝试过:
<Window x:Class="ModalWindow.CustomModalWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Owner="System.Windows.Application.Current.MainWindow">
<!--Some XAML code-->
</Window>
无效。
Owner="System.Windows.Application.Current.MainWindow"
不起作用,因为" system.windows.application.current.mainwindow"只是一个字符串
Window.Owner
不是依赖属性,因此绑定到静态源("{Binding Path=MainWindow, Source={x:Static Application.Current}}"
)也无法正常工作
我像这样修改了App
类:
namespace WpfDemos
{
public partial class App : Application
{
public static Window CurrentMainWindow
{
get { return Current.MainWindow; }
}
}
}
然后在我的窗口中通过{x:Static}
扩展引用该属性:
<Window x:Class="WpfDemos.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfDemos="clr-namespace:WpfDemos"
Owner="{x:Static wpfDemos:App.CurrentMainWindow}"
所以可能是可能的,但是为什么?