Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF DataGrid-获取鼠标光标所在的行号_C#_Wpf_Datagrid - Fatal编程技术网

C# WPF DataGrid-获取鼠标光标所在的行号

C# WPF DataGrid-获取鼠标光标所在的行号,c#,wpf,datagrid,C#,Wpf,Datagrid,我希望获得DataGrid中鼠标光标所在的行号(基本上是在MouseEnter事件上),这样我就可以获得ItemSource也绑定了的DataGridRow项 我对MouseEvent的XAML是 <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}"> <EventSetter Event="MouseEnter" Handler="Eve

我希望获得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>
也许我现在这样做是不可能的,但如果不能用某种方式做到的话,我会感到惊讶


谢谢

好的,我在这里找到了答案

不要被文章的标题弄糊涂了

对于我的解决方案,我基本上使用

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上运行,直到找到正确的元素

如果访问鼠标所在的
DataGridRow
对象,则可以使用以下命令查找其行索引:

这对我有用

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

私有无效行\鼠标指针(对象发送器,鼠标指针)
{
int index=dataGridView.ItemContainerGenerator.IndexFromContainer((DataGridRow)发送方);
}

在我的一个应用程序中,我需要鼠标下方的DataGridRow来突出显示它。 每个单元格都有一个数据模板

<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>
还有这个

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

希望能有所帮助

谢谢Sheridan,VisualTreeHelper在UIElements方面是一个大帮手。以后我需要记住去上这门课。
<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);
}
<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>
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);
    }