Get RoutedEvent Source as grandchild of handler

484 views Asked by At

I'm handling user input in a WPF application, working with RoutedEvents such as MouseDown, MouseMove etc. I've got a good grip of tunneling and bubbling from MSDN's Routed Events Overview, but I've run into a problem with bubbling events.

As a MouseDown event bubbles up to a handler, MouseButtonEventArgs.Source gives me the child of the current element from which the event came. Is there any way to get a hold of the child of that element? I'm thinking something in the way of MouseButtonEventArgs.Source.Source

In this case I'd like to put my input handling as far up in my application as possible, but still get information about the initial element the event came from.

An example below: here the Grid named Grandchild is clicked, and I would like to handle the event in Parent_MouseDown(). However, in that case all the info I get about the source of the event is about the grid named Child. Is there a way to get information about Grandchild from Parent?

<Grid x:Name="Parent" MouseDown="Parent_MouseDown">
    <Grid x:Name="Child" MouseDown="Child_MouseDown">
        <Grid x:Name="Grandchild" MouseDown="Grandchild_MouseDown">
        </Grid>
    </Grid>
</Grid>

Note that this example is simplified, as in my application these elements are of different classes, and reside in different files. Thanks in advance!

1

There are 1 answers

1
Rohit Vats On BEST ANSWER

Try MouseButtonEventArgs.OriginalSource It always point to the original source from where the event bubbled.