you can create WindowsManagerClass
with one static
propery with type of List<Window>
like this
public class WindowsManager
{
public static List<Window> AllWindows = new List<Window>();
}
and add to AllWindows like this code in your Form constructor
public MainWindow()
{
InitializeComponent();
WindowsManager.AllWindows.Add(this);
}
and where you need you can access reference like this
var allwindows = WindowsManager.AllWindows;
var selectedWindows = allwindows.FirstOrDefault(x => x.Name == "Test");
if (selectedWindows != null)
{
if (selectedWindows.IsActive)
{
selectedWindows.Close();
}
}
Full form code (in this example when you click button form will be close)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WindowsManager.AllWindows.Add(this);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var allwindows = WindowsManager.AllWindows;
var selectedWindows = allwindows.FirstOrDefault(x => x.Name == "");
if (selectedWindows != null)
{
if (selectedWindows.IsActive)
{
selectedWindows.Close();
}
}
}
}