Over on Peter McGrattan’s blog is a simple extension method for a DependencyObject which returns the visual tree descending from a particular visual element in a WPF application. The genius in Peter’s simple extension is that it automatically leverages the power of LINQ because the tree is IEnumerable:
But I had a problem: the tree would not descend fully.
To cut a long story short, the issue was caused because VisualTreeHelper.GetChildrenCount doesn’t count the content of a ContentControl as a child.
The following is my version of Peter’s good work – this is just one method cut from the original class, so I implore you to head over to his blog and see the whole thing. I have made the following changes:
- Added support for ContentControl objects.
- Modified the type checking to use IsAssignableFrom so types can be collated using their base classes.
{
return GetVisualTree(element).Where(
}
{
int childrenCount = VisualTreeHelper.GetChildrenCount(element);
if ((childrenCount == 0) && (element is ContentControl))
{
ContentControl cc = element as ContentControl;
if (cc.Content != null)
{
DependencyObject content = cc.Content as DependencyObject;
if (content != null)
{
yield return content;
yield return obj;
}
}
}
for (int i = 0; i < childrenCount; i++)
{
var visualChild = VisualTreeHelper.GetChild(element, i);
yield return visualChild;
{
yield return visualChildren;
}
}
}
0 comments:
Post a Comment