0

如果主 WPF 窗口创建了一个没有分配所有者的无模式窗口,然后它创建了一个模式窗口,为什么无模式窗口会被禁用?这是一个说明问题的代码片段。

xml:

<Window x:Class="ModalTest.MainWindow"
    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"
    Title="MainWindow">
    <Button Content="Show modal window" Click="buttonShowModalWindow_OnClick" />

后面的代码:

using System.Windows;
using System.Windows.Controls;
namespace ModalTest
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            var modelessWindowWithNoOwner = new Window { Content = new TextBlock { Text = "modeless window" } };
            modelessWindowWithNoOwner.Show();
        }

        private void buttonShowModalWindow_OnClick(object sender, RoutedEventArgs e)
        {
            var modalWindowWithOwner = new Window { Owner = this, Content = new TextBlock { Text = "modal window" } };
            modalWindowWithOwner.ShowDialog();
        }
    }
}

谢谢!

4

0 回答 0