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