public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Clear(this.LayoutRoot); } 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); } } 这里面最重要的是VisualTreeHelper类,可以使用它遍历页面元素,它有一些重要方法 如GetParent(),得到父元素。GetChildrenCount()得到子元素数量