Home > Enterprise >  Wpf c# how to close window in page?
Wpf c# how to close window in page?

Time:12-03

I found the problem is It cannot close this window. But it can open the MainWindow. pls help enter image description here Code in button

        private void LoginBtn_Click(object sender, RoutedEventArgs e)
        {
            MainWindow MainView = new MainWindow();
            MainView.Show();

            AuthWindow AuthView = new AuthWindow();
            AuthView.Close();

        }

I want to press the button inside the page and close that window and open another window.

CodePudding user response:

For such scenarios, I advise you to use RoutedCommand. In this case, you can use the ready-made command ApplicationCommands.Close.

In the page button, specify the command name:

    <Button Content="Close Window" Command="Close" />

In the Window, set command executing:

    <Window.CommandBindings>
        <CommandBinding Command="Close" Executed="OnCloseWindow"/>
    </Window.CommandBindings>
    <x:Code>
        <![CDATA[
    private void OnCloseWindow(object sender, ExecutedRoutedEventArgs e)
    {
        this.Close();
    }
        ]]>
    </x:Code>

P.S. I also do not advise you to open new windows. Since you are using Pages, you should change Pages in a single Window. And closing the Window is regarded as an Exit from the Application.

CodePudding user response:

Change

AuthWindow.Close();

To

This.Close();
  • Related