Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This will dismiss some elements - you should extend it like this in order to support a wider array of controls. For a brief discussion, have a look <a href="http://www.hardcodet.net/2008/02/find-wpf-parent" rel="noreferrer">here</a></p> <pre><code> /// &lt;summary&gt; /// Helper methods for UI-related tasks. /// &lt;/summary&gt; public static class UIHelper { /// &lt;summary&gt; /// Finds a parent of a given item on the visual tree. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of the queried item.&lt;/typeparam&gt; /// &lt;param name="child"&gt;A direct or indirect child of the /// queried item.&lt;/param&gt; /// &lt;returns&gt;The first parent item that matches the submitted /// type parameter. If not matching item can be found, a null /// reference is being returned.&lt;/returns&gt; public static T TryFindParent&lt;T&gt;(DependencyObject child) where T : DependencyObject { //get parent item DependencyObject parentObject = GetParentObject(child); //we've reached the end of the tree if (parentObject == null) return null; //check if the parent matches the type we're looking for T parent = parentObject as T; if (parent != null) { return parent; } else { //use recursion to proceed with next level return TryFindParent&lt;T&gt;(parentObject); } } /// &lt;summary&gt; /// This method is an alternative to WPF's /// &lt;see cref="VisualTreeHelper.GetParent"/&gt; method, which also /// supports content elements. Do note, that for content element, /// this method falls back to the logical tree of the element! /// &lt;/summary&gt; /// &lt;param name="child"&gt;The item to be processed.&lt;/param&gt; /// &lt;returns&gt;The submitted item's parent, if available. Otherwise /// null.&lt;/returns&gt; public static DependencyObject GetParentObject(DependencyObject child) { if (child == null) return null; ContentElement contentElement = child as ContentElement; if (contentElement != null) { DependencyObject parent = ContentOperations.GetParent(contentElement); if (parent != null) return parent; FrameworkContentElement fce = contentElement as FrameworkContentElement; return fce != null ? fce.Parent : null; } //if it's not a ContentElement, rely on VisualTreeHelper return VisualTreeHelper.GetParent(child); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload