Silverlight和Visual Tree Manipulation

Now this may be more troublesome than its value. However, it is really useful for me now.

What I want to know is how do I Operate the Silverlight visual tree at runtime. It’s easy to do simple things like adding and removing controls, but when you start traversing the tree with any reasonable complexity, I find myself eager to use JQuery style syntax (I think LINQ also Cool) to handle DOM node replacement, actions, etc.

So I guess the question is, is there any library that can make this job easier, or is there something I missed?

Yes, the Linq extension method is what you are after, but you need to place a small infrastructure first : –

public static class VisualTreeEnumeration
{
public static IEnumerable Descendents(this DependencyObject root, int depth)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; 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 Descendents(this DependencyObject root)
{
return Descendents( root, Int32.MaxValue);
}

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

Now, you can use Linq to query the visual tree using Linq. Some examples:-

// Get all text boxes in usercontrol:-
this.Descendents().OfType();

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

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

// Seek button with name "PinkElephants" even if outside of the current Namescope:-
this. Descendents()
.OfType

Now This may be more troublesome than its value. However, it is really useful for me now.

What I want to know is how I operate Silverlight at runtime Visualize the tree. It’s easy to do simple things like adding and removing controls, but when you start traversing the tree with any reasonable complexity, I find myself eager to use JQuery style syntax (I think LINQ is also cool) to deal with DOM node replacement, actions, etc.

So I guess the question is, is there any library that can make this job easier, or is there something I missed?

Yes, the Linq extension method is what you are after, but you need to place a small infrastructure first: –

public static class VisualTreeEnumeration
{
public static IEnumerable Descendents(this DependencyObject root, int depth)
{
int count = VisualTreeHelper.GetChildrenCount (root);
for (int i = 0; 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 Descendents(this DependencyObject root)
{
return Descendents(root, Int32.MaxValue);
}

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

Now, you can use Linq to query the visual tree using Linq. Some examples: –

/ / Get all text boxes in usercontrol:-
this.Descendents().OfType();

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

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

// Seek button with name "PinkElephants" even if outside of the current Namescope:-
this.Descendents()
.OfType

Leave a Comment

Your email address will not be published.