|
// From http://miguelmadero.blogspot.com/2008/07/use-visualtreehelper-to-navigate_18.html
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
namespace SkinableVideoPlayer
{
public static class VisualTreeExtensions
{
public static IEnumerable<DependencyObject> GetChildren(this DependencyObject depObject)
{
int count = depObject.GetChildrenCount();
for (int i = 0; i < count; i++)
{
yield return VisualTreeHelper.GetChild(depObject, i);
}
}
public static DependencyObject GetChild(this DependencyObject depObject, int childIndex)
{
return VisualTreeHelper.GetChild(depObject, childIndex);
}
public static DependencyObject GetChild(this DependencyObject depObject, string name)
{
return depObject.GetChild(name, false);
}
public static DependencyObject GetChild(this DependencyObject depObject, string name, bool recursive)
{
foreach (var child in depObject.GetChildren())
{
var element = child as FrameworkElement;
if (element != null)
{
if (element.Name == name) // If its a FrameworkElement check Name
return element;
var innerElement = element.FindName(name) as DependencyObject; // Try to get it using FindByName might be more efficient
if (innerElement != null)
return innerElement;
}
if (recursive) // If it's recursive search through its children
{
var innerChild = child.GetChild(name, true);
if (innerChild != null)
return innerChild;
}
}
return null;
}
public static int GetChildrenCount(this DependencyObject depObject)
{
return VisualTreeHelper.GetChildrenCount(depObject);
}
public static DependencyObject GetParent(this DependencyObject depObject)
{
return VisualTreeHelper.GetParent(depObject);
}
}
// From http://forums.silverlight.net/forums/p/19791/67551.aspx
// Canvas[] AllChildren = LayoutRoot.AllChildren().OfType<Canvas>().ToArray();
public static class Extensions
{
public static IEnumerable<UIElement> AllChildren(this Panel panel)
{
foreach (UIElement element in panel.Children)
{
yield return element;
Panel PanelElement = element as Panel;
if (PanelElement != null)
foreach (UIElement Child in PanelElement.AllChildren())
yield return Child;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.