Activate window, close window, bring window to front : Window « Windows Presentation Foundation « C# / CSharp Tutorial






<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();
        }
    }
}
WPF Activate Window Close Window Bring Window To Front








24.70.Window
24.70.1.Window ResizeMode=CanMinimizeWindow ResizeMode=CanMinimize
24.70.2.Set Window Height and WidthSet Window Height and Width
24.70.3.Window OwnershipWindow Ownership
24.70.4.Launch a window with defined XAMLLaunch a window with defined XAML
24.70.5.Hosting a Windows Forms Property Grid in WPFHosting a Windows Forms Property Grid in WPF
24.70.6.Order of precedence for sizing-related properties that are implemented by Window.Order of precedence for sizing-related properties that are implemented by Window.
24.70.7.Animated Video WindowAnimated Video Window
24.70.8.Use WindowState to make full screen window, change resize mode to NoReSizeUse WindowState to make full screen window, change resize mode to NoReSize
24.70.9.Creating the main panel and add to Window in CodeCreating the main panel and add to Window in Code
24.70.10.Is Window activeIs Window active
24.70.11.Load the Data for a Window Asynchronously After It Has RenderedLoad the Data for a Window Asynchronously After It Has Rendered
24.70.12.Change window cursorChange window cursor
24.70.13.Show window based on button nameShow window based on button name
24.70.14.Activate window, close window, bring window to frontActivate window, close window, bring window to front
24.70.15.Load resource from Window ResourcesLoad resource from Window Resources
24.70.16.Save Window Position to RegistrySave Window Position to Registry
24.70.17.Center a Window to ScreenCenter a Window to Screen
24.70.18.Set a Default ButtonSet a Default Button
24.70.19.Use the Grid to create a dialog box that uses the WPF layout APIUse the Grid to create a dialog box that uses the WPF layout API
24.70.20.ShowInTaskbar = falseShowInTaskbar = false
24.70.21.SizeToContent=WidthAndHeightSizeToContent=WidthAndHeight
24.70.22.ResizeMode=CanResizeWithGripResizeMode=CanResizeWithGrip
24.70.23.Set the DataContext of a Window to a person objectSet the DataContext of a Window to a person object
24.70.24.DataContext with user defined objectDataContext with user defined object
24.70.25.Add Button to window content
24.70.26.Not show Window in Taskbar
24.70.27.extends Window
24.70.28.Window loaded event listener
24.70.29.Set window Startup Location
24.70.30.Change window resize mode
24.70.31.Set window start up location
24.70.32.Set WPF window title
24.70.33.Set WPF window width and height
24.70.34.Passing Window instance to Application run method
24.70.35.Listen to window mouse down event
24.70.36.Cast event sender to Window with as
24.70.37.Register and Handle Window event in code
24.70.38.Create Canvas as content of window
24.70.39.Create new windows in Application start up event
24.70.40.Create new window and add mouse down event listener
24.70.41.Display text on to window content
24.70.42.Assign TextBlock to window content