Try something like this in your container class like UserControl, Grid, StackPanel, etc...
public class MyContainerClass : FrameworkElement
{
public MyContainerClass()
{
base.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
m_DataGrid.MouseMove += OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
DataGridRow item = (sender as DependencyObject).ParentOfType<DataGridRow>();
if (item != null && m_DataGrid.SelectedIndex != item.GetIndex())
m_DataGrid.SelectedIndex = item.GetIndex();
}
}
And add this helper class extension...
internal static class DependencyObjectExt
{
// Extension for DependencyObject
internal static TT ParentOfType<TT>(this DependencyObject element) where TT : DependencyObject
{
if (element == null)
return default(TT);
while ((element = VisualTreeHelper.GetParent(element)) != null)
{
if (element is TT)
return (TT)element;
}
return null;
}
}