R – Silverlight 3 Datagrid: Get row/item on MouseOver

datagridmouseoverselectionsilverlight-3.0

I have a bound DataGrid and various other controls(external to the datagrid) that show more details about the selectedrow in the datagrid. This is easy to do with databinding or handling the SelectionChanged event on the datagrid.

However, how do I do this without requiring the user to select a row – eg on 'mouseover' can I change the selected item or get the row/item 'under' the mouse.

Best Answer

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;
    }
}

Good luck,
Jim McCurdy
YinYangMoney