c# - WPF 使用 VisualTreeHelper 垂直和水平查找元素

标签 c# wpf visual-tree visualtreehelper

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

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

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

Visual-Tree

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

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

最佳答案

没有水平搜索,class VisualTreeHelpers谁能帮你Navigate on a WPF’s Visual Tree .通过导航,您可以实现各种搜索。

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

对于实例:

// 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");

关于c# - WPF 使用 VisualTreeHelper 垂直和水平查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32624832/

相关文章:

c# - 将 CMD 输出复制到剪贴板

c# - 如何在服务器 app.config 中保存动态端点?

c# - wpf 窗口刷新首先起作用,然后停止

c# - WPF 容器将所有子控件变为只读

c# - 在 Windows 中对程序进行基准测试的最佳方法是什么?

c# - 在 FluentValidation for WebApi 2 中访问路由数据

c# - 将控件设置为最顶部的 silverlight

wpf - 一旦元素从可视化树中删除,commandBinding 的 CanExecute 如何触发?

c# - Azure + HDInsight 的本地模拟

c# - 在 WPF 中使用 MVVM 打开新窗口的推荐方法是什么