In my application (WPF) i have this window:
public partial class Window1 : Window
and in the Xaml x:Class="WpfApplication1.Window1"
Now, when i switch to and from the main to window 1 and back, i us the Visibility.Hidden and Visibility.Visible to hide them and make them show again to the user.
What i try to do now, is make a test button in the main window, that says: Close Window1.
This window is hidden, but i really want to close it in the background.
at first i though to just use the Window.Close();
but that does not seem to do the trick.
So, how should i do this in a correct way ? Thank you very much in advance.
EDIT 1 - making question more clear
To open the window1 in my Main window, i use this part
Window1 W1 = null; // Initialise Field.
public void CalcTabel_Click(object sender, RoutedEventArgs e)
{
if (W1 == null)
{
W1 = new Window1();
W1.Hoofdmenu = this;
W1.checkLang();
W1.Show();
}
else
{
W1.checkLang();
W1.Visibility = Visibility.Visible;
}
this.Visibility = Visibility.Hidden;
}
On window 1 there is a Back button, that has this snip-it of code in it (Where "Hoofdmenu" us the main window):
Hoofdmenu.updateStatistics();
Hoofdmenu.Visibility = Visibility.Visible;
this.Visibility = Visibility.Hidden;
But again, this time when standing in the main window (so window 1 is hidden) i want to close down that window 1. but using W1.Close()
does not seem to work. So i am looking for a way to Close that window 1, not change its visibility.
EDIT 2 - Solution
So using W1.Close();
did not work, although a small change this.W1.Close();
did work in fact :)
You can create object of Form2 in window and intialize its visiblity to false.
On click of button you can simpliy say
public partial class MainWindow : Window
{
private Window1 window2;
public MainWindow()
{
InitializeComponent();
this.window2 = new Window1();
this.window2.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.window2.Visibility = System.Windows.Visibility.Hidden;
}
}
to make it visible again