博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf z
阅读量:7238 次
发布时间:2019-06-29

本文共 3640 字,大约阅读时间需要 12 分钟。

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)

 

/// /// Finds a parent of a given item on the visual tree./// /// 
The type of the queried item.
/// A direct or indirect child of the/// queried item. ///
The first parent item that matches the submitted /// type parameter. If not matching item can be found, a null /// reference is being returned.
public static T TryFindParent
(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
(parentObject); } } ///
/// This method is an alternative to WPF's ///
method, which also /// supports content elements. Keep in mind that for content element, /// this method falls back to the logical tree of the element! ///
///
The item to be processed. ///
The submitted item's parent, if available. Otherwise /// null.
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
(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:

/// /// Tries to locate a given item within the visual tree,/// starting with the dependency object at a given position. /// /// 
The type of the element to be found/// on the visual tree of the element at the given location.
/// The main element which is used to perform /// hit testing. /// The position to be evaluated on the origin. public static T TryFindFromPoint
(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
(element); }

转载地址:http://rmrfm.baihongyu.com/

你可能感兴趣的文章
Hibernate深入浅出(八)持久层操作——延迟加载(Lazy Loading)
查看>>
wlan加密方式
查看>>
python selenium系列(四)元素等待
查看>>
鸟哥学习笔记---quota
查看>>
我是如何学习NodeJs的 – 笔记
查看>>
Linux日常管理技巧(2):free,ps,netstat命令和抓包工具
查看>>
.NET 4 并行(多核)编程系列之二 从Task开始
查看>>
DiRT2 DirectX 11 Technology
查看>>
C++编程->容器及选用总结
查看>>
6.运算符-未完成
查看>>
王爽《汇编语言第二版》读后感。
查看>>
lamp必备软件包
查看>>
感悟生活-动人!卡西补时向裁判4连喊:要尊重意大利
查看>>
Android studio打包APK
查看>>
晶振电容的选择
查看>>
分析mysql慢查询日志的好工具--mysqlsla
查看>>
Class.isXXX方法
查看>>
常用端口号 及说明
查看>>
uml中六大关系
查看>>
日志无法收缩
查看>>