The RowDefinitions
and ColumnDefinitions
aren't actually in the Visual Tree since they are FrameworkContentElements
(rather than FrameworkElements
) and that's why they don't raise any mouse events, they aren't Visuals
. They are just used by the Grid
to position its childs.
One approach that comes to mind is to use the Attached Events Mouse.MouseMove
and Mouse.MouseLeave
on the Grid
to get notified when these events are raised for any child in the Grid
or the Grid
itself.
<Grid Mouse.MouseMove="Grid_MouseMove"
Mouse.MouseLeave="Grid_MouseLeave"
Background="Transparent">
In the Mouse.MouseMove
event handler we can get the relative mouse position to the Grid
and calculate which RowDefinition
is currently being hoovered by the mouse and store that in an Attached Property, like MouseOverRowDefinition
.
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
Grid grid = sender as Grid;
Point mousePoint = e.GetPosition(grid);
double heightSum = grid.RowDefinitions[0].ActualHeight;
int activeRow = 0;
for (; heightSum < mousePoint.Y; activeRow++)
{
heightSum += grid.RowDefinitions[activeRow].ActualHeight;
}
GridExtensions.SetMouseOverRowDefinition(grid, activeRow);
}
// No RowDefinition is beeing hoovered, set MouseOverRowDefinition to -1
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
Grid grid = sender as Grid;
GridExtensions.SetMouseOverRowDefinition(grid, -1);
}
Now we can query the Grid
for the MouseOverRowDefinition
so the rest is just a matter of comparing Grid.Row
for the Image
to MouseOverRowDefinition
for the Grid
to decide if it should be Visible
or not.
Uploaded a small sample app that does this here if you want to try it out:
http://dl.dropbox.com/u/39657172/MouseOverGridRowDefinition.zip