Visual Studio / WPF > DataGrid > クリックした行のName値を表示する | DataViewRow nullになる

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

以下のクラスから生成したインスタンスをDataGridにて表示しているとする。

```
    public class Person
    {
        public string Name { get; set; }
        public string Race { get; set; }
        public string Codename { get; set; }
    }
```

マウスクリック時に、その行のNameを表示する。

参考: [WPF Datagrid : Need the currently selected Item.](https://social.msdn.microsoft.com/Forums/vstudio/en-US/8daf2f7c-e946-4b8b-8ecc-cb1de9bbe139/wpf-datagrid-need-the-currently-selected-item?forum=wpf)

### code

```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 _170611_t1030_readCsvFile
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

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

        private void DataGrid_mouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataGrid dgc = sender as DataGrid;
            if (dgc == null)
            {
                return;
            }

            Person prsn = dgc.SelectedItem as Person;
            MessageBox.Show(prsn.Name);
        }
    }

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

}
```

```MainWindow.xaml
<Window x:Class="_170611_t1030_readCsvFile.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:_170611_t1030_readCsvFile"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <Style x:Key="keyCellStyle" TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="TabIndex" Value="0">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
                <Trigger Property="TabIndex" Value="1">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <DataGrid Height="300" x:Name="dg1"
                      AutoGenerateColumns="True"
                      IsReadOnly="true"
                      CellStyle="{StaticResource keyCellStyle}"                     
                      MouseDoubleClick="DataGrid_mouseDoubleClick"
                      ItemsSource="{Binding}">
            </DataGrid>
        </StackPanel>
    </Grid>
</Window>
```

2行目をクリックした例
![2017-06-11_16h10_38.png](https://qiita-image-store.s3.amazonaws.com/0/32870/70fa6de6-e0fa-2f5f-a339-79ba86c023d2.png)

### はまった

特定の値(Name)でなく、クリックしたセルの文字列を取得しようとしてはまった。

DataViewRowを使った例が多く見られるが、DataViewRowの値がnullになる症状がある。
https://stackoverflow.com/questions/30020928/datarowview-object-getting-null-value-at-time-of-getting-value-from-datagrid