简体   繁体   中英

How to close a window when user clicks on mainwindow in wpf application

I have a WPF application which shows a window as a DialogBox. I want to close this DialogBox when I click anywhere on MainWindow in application.

Here I am adding one sample example to explain

MainWindow of application

 <Window x:Class="WpfApplication1.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:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Open new Window" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Window> 

Code behind main Window

 namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Window1 temp_Window = new Window1(); temp_Window.ShowDialog(); } } } 

Child window Which I try to close

 <Window x:Class="WpfApplication1.Window1" 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:WpfApplication1" mc:Ignorable="d" Title="Window1" Height="300" Width="300" Deactivated="Window_Deactivated"> <Grid> <TextBlock>New Window</TextBlock> </Grid> </Window> 

Code behind This Child Window

 namespace WpfApplication1 { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Deactivated(object sender, EventArgs e) { Close(); } } } 

Thanks

you can do this with Deactivated event of your DialogBox window

something like this

XAML

<Window x:Class="WpfApplication1.Window1"
        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:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="200" Width="200" WindowStartupLocation="CenterScreen" 
        Deactivated="Window_Deactivated">
    <Grid>

    </Grid>
</Window>

Code

private void Window_Deactivated(object sender, EventArgs e)
{
    this.Close();
}

如果使用ShowDialog调用DialogBox,则背景窗口(即MainWindow)将被禁用,直到您关闭DialogBox,并且它不会获得任何点击,因此除非调用DialogBox,否则您将无法实现所需的功能使用不会锁定背景窗口的Show方法,然后可以在MainWindow GotFocus触发时关闭DialogBox。

You have to declare your second window as a global variable and call the .Hide() and .Show() commands like that:

MainWindow:

public partial class MainWindow : Form
{
    private Dialog m_Dialog;

    public MainWindow()
    {
        InitializeComponent();
        m_Dialog = new Dialog();
        this.Click += closeDialog;
    }

    private void closeDialog(object sender, EventArgs e)
    {
        m_Dialog.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        m_Dialog.Show();
    }
}

It works just fine.

UPDATED :

Here is the same code for WPF. I did it in WinForms previously:

public partial class MainWindow : Window
{
    private Dialog m_Dialog;

    public MainWindow()
    {
        InitializeComponent();
        m_Dialog = new Dialog();
        this.MouseDoubleClick += onCloseDialog;
    }

    private void onCloseDialog(object sender, MouseButtonEventArgs e)
    {
        m_Dialog.Hide();
    }

    private void onButton(object sender, RoutedEventArgs e)
    {
        m_Dialog.Show();
    }
}

You can use another event instead of MouseDoubleClick of course. If you really want to close the window you would have to use the .Close() command and call the constructor of you window everytime.

first declare your temp_window as a filed not as a local variable:

private Window1 temp_Window;

When you open the new window don't open it as a dialog otherwise, the events on the mainWindow will not be handled use Show() instead of ShowDialog() :

private void Button_Click(object sender, RoutedEventArgs e)
        {
            temp_Window = new Window1();
            temp_Window.Show();
        }

On your main window handle the mouseDown event:

MouseDown="MainWindow_OnMouseDown"

and close the tempWindow in the handler

 private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        tempWindow .Close();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM