private static object ExecuteWithChildren(AutomationElement root, WithElementCallback callback)
{
var children = root.FindAll(TreeScope.Children, Condition.TrueCondition);
// Iterate over all the children and call the callback with each one.
// Return the first call that returns a non-null value.
foreach (AutomationElement child in children) {
var result = callback(child);
if (result != null)
return result;
}
// None of the calls returned a value, so return the default value.
return null;
}