WPF 使用 VisualTreeHelper 垂直和水平查找元素 [英] WPF find element with VisualTreeHelper vertical and horizontal

查看:23
本文介绍了WPF 使用 VisualTreeHelper 垂直和水平查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视觉树中垂直和水平搜索最简单的方法是什么?

What is the easiest way, to search vertically and horizontally in the visual tree?

例如我想从控件中找到一个不在父列表中的控件,它开始搜索.

For example I want to find a control which is not in the list of parents from the control, which starts the search.

这是一个简单的例子(每个框代表一些 UI 控件):

Here is a simple example (every box represents some UI control):

例如,我从一个嵌套控件 (Search-Start) 开始,想找到另一个嵌套控件 (应该找到).

For example I start in a nested control (Search-Start) and want to find another nested control (Should be found).

这样做的最佳方法是什么?解析完整的可视化树好像不是很有效……谢谢!

What is the best way to do this? Parsing the complete visual tree seems not to be very effective... Thank you!

推荐答案

没有横向搜索,class VisualTreeHelpers 谁能帮你在 WPF 的可视化树上导航.通过导航,您可以实现各种搜索.

There is no horizontaly search, class VisualTreeHelpers whom can help you Navigate on a WPF’s Visual Tree. Via Navigation you can implement all kinds of searches.

这是最有效的方法,因为它是专门针对您的要求的 .Net 类.

Its the most effective way because its a .Net class specifically for your requirement.

例如:

// Search up the VisualTree to find DataGrid 
// containing specific Cell
var parent = VisualTreeHelpers.FindAncestor<DataGrid>(myDataGridCell);

// Search down the VisualTree to find a CheckBox 
// in this DataGridCell
var child = VisualTreeHelpers.FindChild<CheckBox>(myDataGridCell);

// Search up the VisualTree to find a TextBox 
// named SearchTextBox
var searchBox = VisualTreeHelpers.FindAncestor<TextBox>(myDataGridCell, "SeachTextBox");

// Search down the VisualTree to find a Label
// named MyCheckBoxLabel
var specificChild = VisualTreeHelpers.FindChild<Label>(myDataGridCell, "MyCheckBoxLabel");

这篇关于WPF 使用 VisualTreeHelper 垂直和水平查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆