WPF DataGrid - 获取鼠标光标所在的行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25502150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid - get row number which mouse cursor is on
提问by user3428422
I am looking to get the row number of which the mouse cursor is on in a DataGrid (So basically on a MouseEnter event) so I can get the DataGridRow item of which the ItemSource is binded too,
我希望在 DataGrid 中获取鼠标光标所在的行号(所以基本上是在 MouseEnter 事件上),这样我就可以获得绑定了 ItemSource 的 DataGridRow 项目,
The XAML I have for the MouseEvent is...
我为 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.?
}
Maybe it isnt possible the way I am doing it, but I'd be surprised if it can't be done some way.
也许我这样做是不可能的,但如果不能以某种方式完成,我会感到惊讶。
Thanks
谢谢
回答by Sheridan
If you access the DataGridRowobject that your mouse is over, then you can find its row index using the DataGridRow.GetIndexmethod:
如果访问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();
}
The GetParentOfTypemethod is actually an extension methodthat I use:
该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);
}
回答by user3428422
OK, I found the answer to be here...
好的,我在这里找到了答案......
http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
Don't be confused by the article title..
不要被文章标题搞糊涂了。。
For my solution I basically used
对于我的解决方案,我基本上使用
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) !!!!!!!!!!!!!!!!!
}
- Now you can obtain your own object by using row.Item, and bingo, its on the correct row index
- 现在您可以通过使用 row.Item 和宾果来获取您自己的对象,它位于正确的行索引上
So all about using the mouse original source and going up he VisualTree until you get to the right element.
所以所有关于使用鼠标原始源和向上他 VisualTree 直到你找到正确的元素。
回答by Martin Cerman
This worked for me.
这对我有用。
<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);
}
回答by Michael Bernhard
I need the DataGridRow below the mouse, in one of my applications, to highlight it. Each Cell has a DataTemplate
在我的一个应用程序中,我需要鼠标下方的 DataGridRow 来突出显示它。每个 Cell 都有一个 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>
To get the coordinates of a DataGridCell, I'm looking for an image of the DataTemplate while moving the mouse. If an Image has been found, I'm able to move up the VisualTree to find my cell and it's coordinates (row, column) when I got an Image. It's not a very generic code - just good for my use.
为了获取 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);
}
and this
和这个
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);
}
Hope it helps
希望能帮助到你

