Philipp Sumi recently posted a method that uses VisualTreeHelper.GetParent to find an ancestor of a WPF element. The problem with using VisualTreeHelper.GetParent is that it doesn’t work with “content elements” like Hyperlink, Run, etc., which means that his TryFindFromPoint method would fail if the mouse cursor was over one of those types of elements. We’ve had to write our own GetParent method that traverses through content elements:
We only recently discovered ContentOperations.GetParent; I won’t be surprised if there are other “GetParent” methods hiding in WPF that we’ll eventually need to support as well.
We also have an ancestor-finding method that avoids recursion:
It’s been fun finding so much great WPF content in blogs; I hope the trend continues!
Update: If you want to eliminate the ‘parent’ and ‘fce’ temporary variables from GetParent, you can use our IfNotNull extension method and replace that block of code with:
Posted by Ed Ball on February 06, 2008