Finding an ancestor of a WPF dependency object

This is a simple snippet which helps you to find a specified parent of a given WPF dependency object somewhere in its visual tree:

(Snippet updated 2009.09.14)

/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the
/// queried item.</param>
/// <returns>The first parent item that matches the submitted
/// type parameter. If not matching item can be found, a null
/// reference is being returned.</returns>
public static T TryFindParent<T>(this 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<T>(parentObject);
}
} /// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
/// supports content elements. Keep in mind that for content element,
/// this method falls back to the logical tree of the element!
/// </summary>
/// <param name="child">The item to be processed.</param>
/// <returns>The submitted item's parent, if available. Otherwise
/// null.</returns>
public static DependencyObject GetParentObject(this DependencyObject child)
{
if (child == null) return null; //handle content elements separately
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;
} //also try searching for parent in framework elements (such as DockPanel, etc)
FrameworkElement frameworkElement = child as FrameworkElement;
if (frameworkElement != null)
{
DependencyObject parent = frameworkElement.Parent;
if (parent != null) return parent;
} //if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}

This snippet works with arbitrary dependency objects that are of Type Visual or Visual3D. So let’s say you need a reference to the Window that hosts a given Button control somewhere, all you need is this:

Button myButton = ...
Window parentWindow = UIHelper.TryFindParent<Window>(myButton);

The above TryFindParent method also makes it easy to get an item at a given position. The method below performs a hit test based on a given position. If hit testing does not return the requested item (e.g. a clicked CheckBox on a tree, while you are keen on the TreeViewItem that hosts the CheckBox), the procedure delegates the lookup to TryFindParent.

This comes in very handy for mouse-related events if you just need to now what’s under your mouse pointer:

/// <summary>
/// Tries to locate a given item within the visual tree,
/// starting with the dependency object at a given position.
/// </summary>
/// <typeparam name="T">The type of the element to be found
/// on the visual tree of the element at the given location.</typeparam>
/// <param name="reference">The main element which is used to perform
/// hit testing.</param>
/// <param name="point">The position to be evaluated on the origin.</param>
public static T TryFindFromPoint<T>(UIElement reference, Point point)
where T:DependencyObject
{
DependencyObject element = reference.InputHitTest(point)
as DependencyObject;
if (element == null) return null;
else if (element is T) return (T)element;
else return TryFindParent<T>(element);
}

最新文章

  1. Angular2学习之开发环境构建
  2. 论文笔记之: Person Re-Identification by Multi-Channel Parts-Based CNN with Improved Triplet Loss Function
  3. PHP 错误与异常 笔记与总结(12 )异常
  4. MVC常用 ActionResult
  5. Java通过JDBC连接Oracle之后查询结果和在sqlplus查询结果不一样
  6. oracle添加数据时主键自动增长
  7. HDU5126---stars (CDQ套CDQ套 树状数组)
  8. CSS基础知识笔记(三)
  9. asp Eval()函数的一些使用总结
  10. vmware时间不同步的问题
  11. CSS3 学习小结
  12. uvalive 3135 Argus
  13. SSH/HTTPS安全的本质都依赖于TLS/SSL
  14. face recognition[Euclidean-distance-based loss][Center Face]
  15. python---反射详解
  16. CVE-2018-14424 use-after-free of disposed transient displays 分析报告
  17. 20155312 2006-2007-2 《Java程序设计》第三周学习总结
  18. python技巧 合并两个字典
  19. 一次dns缓存引发的慘案
  20. ASP.NET车辆管理系统

热门文章

  1. Jenkins+Docker持续集成
  2. VS 2015 序列号/密钥/企业版/专业版
  3. Asp.net Vnext TagHelpers
  4. uc 下载页面 记录备份
  5. js 获取时间戳的方法
  6. thinkphp结合ajax实现统计页面pv的浏览量
  7. [js]DOM 篇
  8. 四、django rest_framework源码之频率控制剖析
  9. Visual Studio 2017强制更新方法
  10. 使用 Python 读取火狐的 cookies