在我的WPF项目中,我有两个Windows,在主父窗口中,我按如下方式调用子窗口:
亲本
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();
}我希望这样,当用户单击父窗口时,它会关闭子窗口并聚焦于父窗口。我在执行Deactivated的子窗口上做了一个Close();事件,但是单击父窗口时,它会关闭窗口,但也会隐藏父窗口,因此它不是聚焦的,而是在所有其他应用程序下。我要怎么做才能不这样做呢?我尝试过在owner.Activate();关闭后拥有它,但是同样的事情也会发生。
这是一个关于正在发生的事情的小视频:https://vimeo.com/485043728。我希望在单击另一个窗口(其父窗口可见)时获得同样的效果,但是当用户单击回父窗口时,它会隐藏它。
发布于 2020-12-16 14:49:07
你可以这样做来达到你想要的结果。
MainWindow.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>MainWindow.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();
}
}
}https://stackoverflow.com/questions/65054678
复制相似问题