You can list the visual tree and check if a property is set for each of the elements. Something like that:
var objectsWithPropertySet = new List<DependencyObject>();
if (RootVisual.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue)
objectsWithPropertySet.Add(RootVisual);
objectsWithPropertySet.AddRange(RootVisual.GetAllChildren()
.Where(o => o.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue));
The GetAllChildren () extension method is implemented as follows:
public static class VisualTreeHelperExtensions
{
public static IEnumerable<DependencyObject> GetAllChildren(this DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var directChild = (Visual)VisualTreeHelper.GetChild(parent, i);
yield return directChild;
foreach (var nestedChild in directChild.GetAllChildren())
yield return nestedChild;
}
}
}
source
share