簡體   English   中英

關閉 Window 關閉並關注父 Window WPF

[英]Close Window on Deactivated and Focus on Parent Window WPF

在我的 WPF 項目中,我有兩個Windows和來自主要父母 Window 我這樣稱呼孩子:

家長

ChildWindow win = new ChildWindow();
win.Owner = this /* (Parent window) */;
win.Show();

孩子

private void Window_Deactivated(object sender, EventArgs e)
{
    this.Close();
    owner.Activate();
    // Also tried: owner.Focus();
}

我想要這樣當用戶點擊父 window 時,它會關閉子 window 並專注於父。 我在執行Close();的孩子身上做了一個Deactivated事件。 但是在單擊父 window 時,它會關閉 window 但也會隱藏父級,因此它不是集中的,而是在所有其他應用程序下。 我怎樣才能讓它不呢? 我試過擁有一個owner.Activate(); 關閉后但同樣的事情發生。

這是正在發生的事情的一個小視頻: https://vimeo.com/485043728 我想在單擊另一個 window(父 window 可見)時獲得相同的效果,但是當用戶單擊返回父 window 時,它會隱藏它。

你可以為你想要的結果做這樣的事情。

主窗口.xaml

<Window x:Class="StackOverflow.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"
        xmlns:local="clr-namespace:StackOverflow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Height="50" Width="100" Content="Open Child" Click="Button_Click"/>
    </Grid>

主窗口.xaml.cs

using System.Windows;

namespace StackOverflow
{
    public partial class MainWindow : Window
    {
        private Window _child;

        public MainWindow()
        {
            InitializeComponent();
            _child = new Window();
            _child.LostKeyboardFocus += _child_LostFocus;
        }

        private void _child_LostFocus(object sender, RoutedEventArgs e)
        {
            _child.Hide();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _child.Show();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM