Clear all the text boxes in a hierarchy of elements in Silverlight

private void Clear(DependencyObject element) 
{ 
    // If this is a text box, clear the text. 
    TextBox txt = element as TextBox; 
    if (txt ! = null) txt. Text = " "; 
 
    // Check for nested children. 
    int children = VisualTreeHelper.GetChildrenCount(element) ; 
    for (int i = 0; i < children; i++) 
    { 
        DependencyObject child = VisualTreeHelper.GetChild(element, i); 
        Clear(child); 
    } 
}

To use the above code, call the Clear() method with the topmost object you want to examine. Here’s how to affect the entire current page:

Clear(this) ;

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.