I tried to find the answer to this problem, and in every post I found that there is an answer to the search recursively, but none of them work with hidden or collapsed children.
Also, in each post, someone asked if this was possible, but no one answered, so I'm starting to think that this is impossible.
If anyone has a way to do this, I will be forever grateful.
My function looks like this:
public static DependencyObject FindLogicalDescendentByName(this DependencyObject source, string name)
{
DependencyObject result = null;
IEnumerable children = LogicalTreeHelper.GetChildren(source);
foreach (var child in children)
{
if (child is DependencyObject)
{
if (child is FrameworkElement)
{
if ((child as FrameworkElement).Name.Equals(name))
result = (DependencyObject)child;
else
result = (child as DependencyObject).FindLogicalDescendentByName(name);
}
else
{
result = (child as DependencyObject).FindLogicalDescendentByName(name);
}
if (result != null)
return result;
}
}
return result;
}
-EDITED So, I understand that the problem is that I tried to find the item before it was created,
I was attached to a property in xaml that would go away and find the element by the given name, but the element was not created at this point in time, if I re-order the element in xaml, it works and the element is found ... doh!
Kezza source