Get Grid Cell by mouse click

后端 未结 4 1390
逝去的感伤
逝去的感伤 2020-12-20 14:29

I have a WPF Grid which is divided into 3 rows and 3 columns, i wasn\'t able to find a way of getting the row and column number of mouse click on the net, ohh and if it is p

4条回答
  •  Happy的楠姐
    2020-12-20 15:03

    I use something like this:

    private void DataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // Check if the user double-clicked a grid row and not something else
            if (e.OriginalSource == null) return;
            var row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
    
            // If so, go ahead and do my thing
            if (row != null)
            {
                var Item = (CLASS_YOU_USE_TO_BIND)DataGrid1.Items[row.GetIndex()];
    //Here you can work with Item, it is now the object of class you used in
    //DataGrid.DataSource
            }
    }
    

提交回复
热议问题