Mega Code Archive
Activate window, close window, bring window to front
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window1_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 1; i <= 5; i++)
{
Window window = new Window();
SetupWindow(window, "Window " + i);
window.Show();
}
RebuildWindowList();
}
private void SetupWindow(Window window, string title)
{
window.Closing += new CancelEventHandler(Window_Closing);
window.Closed += new EventHandler(Window_Closed);
window.Title = title;
window.Width = 100d;
window.Height = 100d;
window.Content = title;
}
private void RebuildWindowList()
{
List windows = new List();
foreach (Window window in Application.Current.Windows)
{
if (window == this)
continue;
windows.Add(window);
}
lbxWindows.ItemsSource = windows;
}
private void Window_Closed(object sender, EventArgs e)
{
RebuildWindowList();
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Window w = sender as Window;
if (w == null)
return;
e.Cancel = !cbxCanClose.IsChecked == true;
}
private void CheckBox_Checked_Changed(object sender, RoutedEventArgs e)
{
Window window = lbxWindows.SelectedItem as Window;
if (window == null)
return;
if (cbxIsVisible.IsChecked == true)
window.Show();
else
window.Hide();
}
private void btnBringToFront_Click(object sender, RoutedEventArgs e)
{
Window window = lbxWindows.SelectedItem as Window;
if (window != null)
window.Activate();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Window window = lbxWindows.SelectedItem as Window;
if (window != null)
window.Close();
}
}
}