Adtapum Mayomthong
Adtapum Mayomthong

Reputation: 3

Wpf c# how to close window in page?

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.

Upvotes: 0

Views: 97

Answers (1)

EldHasp
EldHasp

Reputation: 7908

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.

Upvotes: 1

Related Questions