Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between two windows in WPF

Tags:

c#

wpf

Suppose i have two different windows in WPF application Window1 and Window2.

Based on some actions from Window1, Window2 pop up for a while, and also based on some actions in Window1, Window2 should close and view returns to Window1 with it's state before Window2 appears.

With it's state i mean with all the content in all the controls that exists in Window1.

To achieve the switching i used

ShowDialog();

which i was looking exactly because i need Window1 to freeze while Window2 is on.

Right now my problem i can't return to Window1 with it's content.

MainWindow (Window1).xaml

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="193,130,0,0" Click="Button_Click"/>
</Grid></Window>

Button_Click

private void Button_Click(object sender, RoutedEventArgs e)
{
    Window1 window1 = new Window1();
    window1.ShowDialog();

    //Actions

    window1.Close();
}

and the Window1 xaml is a normal one i didn't change anything except that i made the

WindowStyle=None

so i can't exit it with the right top exit button

like image 373
Xexolas Avatar asked Oct 29 '25 19:10

Xexolas


1 Answers

This topic is too old but perhaps this is what you were talking about?

// Currently in Window1, want to open window2 and make window1 not visible
private void Change_Click(object sender, RoutedEventArgs e)
        {
            Window2 mainWindow = new Window2();
            Visibility = Visibility.Hidden;
            Window2.Show();
        }
like image 118
Nam.tsh Avatar answered Nov 01 '25 10:11

Nam.tsh