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,
The XAML I have for the MouseEvent is...
<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
If you access the DataGridRow
object that your mouse is over, then you can find its row index using the DataGridRow.GetIndex
method:
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 GetParentOfType
method is actually an extension method that I use:
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);
}
OK, I found the answer to be here...
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) !!!!!!!!!!!!!!!!!
}
So all about using the mouse original source and going up he VisualTree until you get to the right element.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With