In my WPF application, I have a main window (Branch.xaml) which has a button that will open an other window (Location.xaml). Once this Location window is open, how do I prevent another instance of this Location window from opening, when the user clicks the same button again?
Or how can I re-focus the same open window, when the user clicks the button again?
The button click code is auto-generated code when you double-click on a button in xaml.
In the “Branch.xaml.cs” file, the code for button click is as follows:
private void rbtn_Location_Click(object sender, RoutedEventArgs e)
{
Location location = new Location();
location.Show();
}
Location is a custom class which opens a window with 3 list boxes
Thanks, any help is appreciated.
I’m using WPF application on C# 4.0 & Visual Studio 2010.
You could create a field in your main window which holds a reference to the location window if there is any, in the button click handler check if the field is null and if it is create a new window and store it in the field, if not call
Activateon the window in the field. You also would have to subscribe to theClosedevent of the location window to clear out the reference again when the location window is gone.Edit: Concrete example: