Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

c# - How to set window's owner to MainWindow through XAML in WPF?

How can I set window's owner to Application.Current.MainWindow through XAML? So far I've tried this:

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

That didn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Owner="System.Windows.Application.Current.MainWindow" won't work because "System.Windows.Application.Current.MainWindow" is just a string

Window.Owner isn't a dependency property, so binding to static source ("{Binding Path=MainWindow, Source={x:Static Application.Current}}") won't work also

I modified App class like this:

namespace WpfDemos
{
    public partial class App : Application
    {
        public static Window CurrentMainWindow
        {
            get { return Current.MainWindow; }
        }
    }
}

and then in my window referenced that property via {x:Static} extension:

<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}"

so it is possible, but why?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...