Not sure this is the best solution, but it seems to work so far.
Here's a generic helper method:
private T GetParentFromVisualTree<T>( DependencyObject dependencyObject ) where T : DependencyObject
{
// Iteratively traverse the visual tree
while ( dependencyObject != null && !( dependencyObject is T ) )
dependencyObject = VisualTreeHelper.GetParent( dependencyObject );
if ( dependencyObject == null )
return null;
return dependencyObject as T;
}
I then use it in the Row_MouseRightButonDown event as per the other StackOverflow question referenced above:
private void DataGrid_LoadingRow( object sender, DataGridRowEventArgs e )
{
e.Row.MouseRightButtonDown += Row_MouseRightButtonDown;
}
private void DataGrid_UnloadingRow( object sender, DataGridRowEventArgs e )
{
e.Row.MouseRightButtonDown -= Row_MouseRightButtonDown;
}
private void Row_MouseRightButtonDown( object sender, MouseButtonEventArgs e )
{
var dataGridRow = sender as DataGridRow;
if (dataGridRow == null)
return;
// Select the row
DataGrid.SelectedItem = dataGridRow.DataContext;
// Select the column
var dataGridCell = GetParentFromVisualTree<DataGridCell>( e.OriginalSource as DependencyObject );
if ( dataGridCell != null )
{
var dataGridColumn = DataGridColumn.GetColumnContainingElement( dataGridCell );
DataGrid.CurrentColumn = dataGridColumn;
}
}