WPF数据网格 - 获取鼠标光标所在的行号

7

我希望能够在DataGrid中获取鼠标光标所在行的行号(基本上是在MouseEnter事件中),以便我可以获取绑定到ItemSource的DataGridRow项。

我的MouseEvent的XAML如下...

   <DataGridTextColumn.ElementStyle>
      <Style TargetType="{x:Type TextBlock}">
         <EventSetter Event="MouseEnter" Handler="Event"></EventSetter>
         <Setter Property="ToolTip" Value="{Binding Property}" />
      </Style>
   </DataGridTextColumn.ElementStyle>

The event itself...

  private void Event(object sender, MouseEventArgs e)
  {   
     // I have the DataGrid object itself.
     m_DataGrid.?
  }

也许我现在的做法不太可行,但是如果有其他方法,我会感到惊讶。

谢谢。

4个回答

16

如果你访问鼠标悬停的 DataGridRow 对象,那么你可以使用 DataGridRow.GetIndex 方法 找到它的行索引:

private void Event(object sender, MouseEventArgs e)
{
    HitTestResult hitTestResult = 
        VisualTreeHelper.HitTest(m_DataGrid, e.GetPosition(m_DataGrid));
    DataGridRow dataGridRow = hitTestResult.VisualHit.GetParentOfType<DataGridRow>();
    int index = dataGridRow.GetIndex();
}

GetParentOfType 方法实际上是我使用的一个扩展方法:

public static T GetParentOfType<T>(this DependencyObject element) where T : DependencyObject
{
    Type type = typeof(T);
    if (element == null) return null;
    DependencyObject parent = VisualTreeHelper.GetParent(element);
    if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) parent = ((FrameworkElement)element).Parent;
    if (parent == null) return null;
    else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) return parent as T;
    return GetParentOfType<T>(parent);
}

谢谢Sheridan,VisualTreeHelper在处理UI元素方面是一个很大的帮助器。我将需要记住在未来使用这个类。 - user3428422

1

好的,我发现答案在这里...

http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html

不要被文章标题所迷惑...
对于我的解决方案,我基本上使用了
private void MouseOverEvent(object sender, MouseEventArgs e)
{
        DependencyObject dep = (DependencyObject)e.OriginalSource;

         // iteratively traverse the visual tree
         while ((dep != null) &&
                 !(dep is DataGridCell) &&
                 !(dep is DataGridColumnHeader))
         {
            dep = VisualTreeHelper.GetParent(dep);
         }

         if (dep == null)
            return;

         if (dep is DataGridColumnHeader)
         {
            DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
            // do something
         }

         if (dep is DataGridCell)
         {
            DataGridCell cell = dep as DataGridCell;

            // navigate further up the tree
            while ((dep != null) && !(dep is DataGridRow))
            {
               dep = VisualTreeHelper.GetParent(dep);
            }

            DataGridRow row = dep as DataGridRow;

           //!!!!!!!!!!!!!* (Look below) !!!!!!!!!!!!!!!!!

         }

现在,您可以通过使用row.Item获得自己的对象,并且 bingo,它位于正确的行索引上。 所以,全部关于使用鼠标原始源并向上遍历VisualTree直到找到正确的元素。

1
这对我有效。
<DataGrid x:Name="dataGridView">
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseEnter" Handler="Row_MouseEnter"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>

private void Row_MouseEnter(object sender, MouseEventArgs e)
{
    int index = dataGridView.ItemContainerGenerator.IndexFromContainer((DataGridRow)sender);
}

0
我需要在我的一个应用程序中,获取鼠标下方的DataGridRow并将其高亮显示。 每个单元格都有一个DataTemplate。
<DataTemplate x:Key="templateCenter">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10 0 10 0">
            <Image Source="{StaticResource Back}" Width="15" Height="15"/>
            <Image Source="{StaticResource ListMove}" Width="35" Height="35"/>
            <Image Source="{StaticResource Forward}" Width="15" Height="15"/>
        </StackPanel>
    </DataTemplate>

要获取DataGridCell的坐标,我正在寻找移动鼠标时DataTemplate的图像。 如果找到了图像,我就可以向上移动VisualTree以找到我的单元格及其坐标(行、列)。 这不是非常通用的代码 - 只适用于我的使用。
private void DragDrop_PreviewDragOver(object sender, DragEventArgs e)
    {
        if (e.OriginalSource.GetType() != typeof(Image))
        {
            return;
        }

        Point destination = GetCellLocation((DataGrid)sender, (StackPanel)((Image)e.OriginalSource).Parent);
    }

和这个

private Point GetCellLocation(DataGrid datagrid, StackPanel stackpanel)
    {
        DataGridCell cell = (DataGridCell)((ContentPresenter)stackpanel.TemplatedParent).Parent;
        int column = cell.Column.DisplayIndex;
        int row = ((DataGrid)datagrid).Items.IndexOf(stackpanel.DataContext);

        return new Point(column, row);
    }

希望能有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接