Silverlight 3 Datagrid: Get row/item on MouseOver

烂漫一生 提交于 2019-12-10 10:50:00

问题


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.


回答1:


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




回答2:


Here is a simpler but less generic implementation of Jim's answer. In VB.Net:

Private Sub DataGrid1_LoadingRow(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridRowEventArgs) Handles DataGrid1.LoadingRow
    AddHandler e.Row.MouseEnter, AddressOf row_MouseEnter
End Sub

Private Sub row_MouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim row = CType(sender, DataGridRow)
        Me.DataGrid1.SelectedItem = CType(row.DataContext, MyType)
End Sub


来源:https://stackoverflow.com/questions/1866189/silverlight-3-datagrid-get-row-item-on-mouseover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!