gpt4 book ai didi

Silverlight 和可视化树操作

转载 作者:行者123 更新时间:2023-12-04 23:31:32 25 4
gpt4 key购买 nike

现在这个 5 月 比它值得的麻烦更多,但尽管如此,它现在对我来说真的很有用。

我想知道的是如何在运行时操作 Silverlight 可视化树。做一些简单的事情,比如添加和删除控件是很容易的,但是当你开始不得不以任何合理的复杂度遍历树时,我发现自己渴望使用 JQuery 风格的语法(我认为 LINQ 也很酷)来处理 DOM 节点替换、 Action 等。

所以我想问题是是否有任何图书馆可以使这项工作更轻松,或者是否有一些我错过的东西?

最佳答案

是的,Linq 扩展方法正是您所追求的,但您需要先安装一个小型基础设施:-

public static class VisualTreeEnumeration
{
public static IEnumerable<DependencyObject> Descendents(this DependencyObject root, int depth)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
if (depth > 0)
{
foreach (var descendent in Descendents(child, --depth))
yield return descendent;
}
}
}

public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
{
return Descendents(root, Int32.MaxValue);
}

public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
{
DependencyObject current = VisualTreeHelper.GetParent(root);
while (current != null)
{
yield return current;
current = VisualTreeHelper.GetParent(current);
}
}
}

现在您可以使用 Linq 查询使用 Linq 的可视化树。一些例子:-
 // Get all text boxes in usercontrol:-
this.Descendents().OfType<TextBox>();

// All UIElement direct children of the layout root grid:-
LayoutRoot.Descendents(0).OfType<UIElement>();

// Find the containing `ListBoxItem` for an element:-
elem.Ancestors().OfType<ListBoxItem>.FirstOrDefault();

// Seek button with name "PinkElephants" even if outside of the current Namescope:-
this.Descendents()
.OfType<Button>()
.FirstOrDefault(b => b.Name == "PinkElephants");

关于Silverlight 和可视化树操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4428130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com