在检索单元格内容之前,您可能需要检查当前选定的计数是否不为零。只是一个小代码来帮助你。
private void dtgTarafAvval_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if(dtgTarafAvval.SelectedCells.Count > 0)
{
CellValue = GetSelectedValue(dtgTarafAvval);
//CellValue is a variable of type string.
}
}
private string GetSelectedValue(DataGrid grid)
{
DataGridCellInfo cellInfo = grid.SelectedCells[0];
if (cellInfo == null) return null;
DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn;
if (column == null) return null;
FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item };
BindingOperations.SetBinding(element, TagProperty, column.Binding);
return element.Tag.ToString();
}
PS:以上代码适用于SelectionUnit='FullRow'