Visual Studio / WPF > DataGrid > ボタン押下時にTextBoxに入力した項目を追加する

```txt:動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
```

http://qiita.com/7of9/items/d66f0f76e2f696a9179e
の続き。

レコードを追加する処理を実装。

- uxName:TextBox 追加
- uxRace:TextBox 追加
- uxCodeName:TextBox 追加
- uxAdd:Button 追加
    - 押下時に上記の3項目をセットしたレコードをmyListに追加する
    - 空白チェックを行う

```MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//
using System.IO;
using System.Globalization;
using System.Data;
using System.Collections.ObjectModel;

namespace _170612_t1420_addRecord
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    { 
        private ObservableCollection<Person> myList;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            myList = new ObservableCollection<Person>();
            myList.Add(new Person { Checked = true, Name = "7of9", Race = "Borg", Codename = "seven" });
            myList.Add(new Person { Checked = false, Name = "Janeway", Race = "Human", Codename = "Captain" });
            myList.Add(new Person { Checked = true, Name = "Odo", Race = "Unknown", Codename = "Odo" });
            this.DataContext = myList;
        }

        private void dg1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Checked")
            {
                //e.Cancel = true; // 非表示
                e.Column.IsReadOnly = false;
                e.Column.Header = "";
            } else
            {
                e.Column.IsReadOnly = true;
            }
        }

        private void uxDelete_Click(object sender, RoutedEventArgs e)
        {
            var removeList = myList.Where(x => x.Checked).ToList();
            foreach (var item in removeList)
            {
                myList.Remove(item);
            }
        }

        private void uxAdd_Click(object sender, RoutedEventArgs e)
        {
            //if (uxName.Text is null || 
            //    uxRace.Text is null ||
            //    uxCodename.Text is null)
            //{
            //    return; // error
            //}

            if (uxName.Text.Length == 0 ||
                uxRace.Text.Length == 0 ||
                uxCodename.Text.Length == 0) {
                return; // error
            }
                
            var data = new Person
            {
                Checked = false,
                Name = uxName.Text,
                Race = uxRace.Text,
                Codename = uxCodename.Text
            };
            myList.Add(data);
        }
    }

    // Name, Race, Codename
    public class Person
    {
        public bool Checked { get; set; }
        public string Name { get; set; }
        public string Race { get; set; }
        public string Codename { get; set; }
    }

}
```

```MainWindow.xaml
<Window x:Class="_170612_t1420_addRecord.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170612_t1420_addRecord"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <StackPanel Margin="5">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name"/>
                <TextBox x:Name="uxName" Width="100"/>
                <TextBlock Text="Race"/>
                <TextBox x:Name="uxRace" Width="100"/>
                <TextBlock Text="Codename"/>
                <TextBox x:Name="uxCodename" Width="100"/>
                <Button x:Name="uxAdd" Content="Add" Width="70"
                    Click="uxAdd_Click"/>
            </StackPanel>
            <Button x:Name="uxDelete" Content="delete"
                    Click="uxDelete_Click" Width="100"/>
            <DataGrid Height="300" x:Name="dg1"
                      AutoGenerateColumns="True"
                      IsReadOnly="False"
                      CanUserAddRows="False"
                      ItemsSource="{Binding}" AutoGeneratingColumn="dg1_AutoGeneratingColumn"/>
        </StackPanel>
    </Grid>
</Window>
```

初期画面
![2017-06-12_15h10_58.png](https://qiita-image-store.s3.amazonaws.com/0/32870/fe8fd83f-669c-0c10-bb2b-7179e2184ad2.png)

Neelix追加後
![2017-06-12_15h11_31.png](https://qiita-image-store.s3.amazonaws.com/0/32870/20950519-6490-b718-9494-e84920256705.png)

OdoはVoyagerのメンバーではなかった。。。