I am trying to determine which row (RowDefinition) my mouse is over within a WPF grid.
I have tried adding the MouseEnter event to the RowDefinition but the event doesn't fire, the Event does fire on the Grid itself but that doesn't help as I need the name of the row the mouse is currently over.
Thanks in advance.
Have your handler on each element, not on the grid itself. E.g. if you have TextBlocks
there you can set handler using style:
<Grid Name="_grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<EventSetter Event="MouseEnter" Handler="EventSetter_OnHandler" />
</Style>
</Grid.Resources>
<TextBlock>a</TextBlock>
<TextBlock Grid.Row="1">b</TextBlock>
<TextBlock Grid.Row="2">c</TextBlock>
</Grid>
Then within handler you know the element from MouseEventArgs.Source
. Do GetValue(Grid.RowProperty)
if you need to find out row number and if you really need RowDefinition
, get it from Grid.RowDefinitions
:
private void EventSetter_OnHandler(object sender, MouseEventArgs e)
{
var element = (FrameworkElement) e.Source;
var rowNumber = (int) element.GetValue(Grid.RowProperty);
RowDefinition rowDefinition = _grid.RowDefinitions[rowNumber];
e.Handled = true;
}