<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:system="clr-namespace:System.Windows;assembly=PresentationFramework"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="310" Width="280" Loaded="Window1_Loaded">
<Window.Resources>
<DataTemplate DataType="{x:Type Window}" x:Key="WindowTemplate">
<StackPanel>
<Rectangle Height="50" Width="50">
<Rectangle.Fill>
<VisualBrush Visual="{Binding}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{Binding Path=Title}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox x:Name="lbxWindows" ItemTemplate="{StaticResource WindowTemplate}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<StackPanel Grid.Row="1">
<CheckBox x:Name="cbxIsVisibleInTaskBar" Content="IsVisibleInTaskbar" IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.ShowInTaskbar}"/>
<CheckBox x:Name="cbxIsVisible" Content="IsVisible"
IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.IsVisible, Mode=OneWay}"
Checked="CheckBox_Checked_Changed"
Unchecked="CheckBox_Checked_Changed"/>
<CheckBox x:Name="cbxCanClose" Content="CanClose" IsChecked="True"/>
<Button Content="Bring To Front" Click="btnBringToFront_Click" />
<Button Content="Close" Click="btnClose_Click"/>
</StackPanel>
</Grid>
</Window>
//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<Window> windows = new List<Window>();
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();
}
}
}
|