Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

ウィンドウを管理する

MainWindow.xaml

<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="200"
        SizeToContent="Height">
    <Grid>
        <StackPanel>
            <Button Click="NewWindowCliecked">New Window Cliecked</Button>
            <Button Click="ListOpenWindow">List OpenWindow</Button>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Text;
using System.Windows;

namespace HelloWorld {
    public partial class MainWindow : Window {

        static int _createcount;
        
        public MainWindow() {
            InitializeComponent();
            Title = "ウィンドウ" + _createcount;
            _createcount++;
        }

        /// <summary>
        /// 新しいウィンドウを生成する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewWindowCliecked(object sender, RoutedEventArgs e) {
            new MainWindow().Show();
        }

        /// <summary>
        /// ウィンドウのリストを表示する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ListOpenWindow(object sender, RoutedEventArgs e) {
            StringBuilder sb = new StringBuilder();
            foreach (Window w in Application.Current.Windows) {
                sb.AppendLine(w.Title);
            }
            MessageBox.Show(sb.ToString(), "Open Window");
        }
    }
}

検証