LoginSignup
12
11

More than 5 years have passed since last update.

WPF DataGridでダブルクリックしたセルを得る

Last updated at Posted at 2015-10-09

DataGridでダブルクリックされたセルをお手軽に取得する方法。

MouseDoubleClickイベントでマウスカーソル下のFrameworkElementを得れば対象のセルまで辿り着けます。

xaml
<DataGrid ItemsSource="{Binding dataSource}" MouseDoubleClick="DataGrid_MouseDoubleClick">
(略)
</DataGrid>
コードビハインド
private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var elem = e.MouseDevice.DirectlyOver as FrameworkElement;
    if (elem != null)
    {
        DataGridCell cell = elem.Parent as DataGridCell;
       if (cell == null)
        {
             // ParentでDataGridCellが拾えなかった時はTemplatedParentを参照
             // (Borderをダブルクリックした時)
             cell = elem.TemplatedParent as DataGridCell;
        }
        if (cell != null)
        {
            // ここでcellの内容を処理
            // (cell.DataContextにバインドされたものが入っているかと思います)
        }
    }
}
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11