2011-03-29 33 views

回答

0

下一頁功能將在指定的對象指定類型的所有控件搜索:

/// <summary> 
/// Helper function for searching all controls of the specified type. 
/// </summary> 
/// <typeparam name="T">Type of control.</typeparam> 
/// <param name="depObj">Where to look for controls.</param> 
/// <returns>Enumerable list of controls.</returns> 
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) 
    where T : DependencyObject 
{ 
    if (depObj != null) 
    { 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
      if (child != null && child is T) 
      { 
       yield return (T)child; 
      } 

      foreach (T childOfChild in FindVisualChildren<T>(child)) 
      { 
       yield return childOfChild; 
      } 
     } 
    } 
} 

由於這項功能的作者......不記得在那裏我把它。現在

可以爲所有的文本框例子清晰的價值觀:

foreach (TextBox child in FindVisualChildren<TextBox>(yourGrid)) 
{ 
    child.Text = string.Empty; 
} 
3

清除文本下面的代碼應該清除所有文本框:

var textboxes = grid.Children.OfType<TextBox>(); 
foreach (var textBox in textboxes) 
    textBox.Text = String.Empty; 
+0

我修改的問題,瞭解我想 – kartal 2011-03-29 14:35:40

+0

編輯我的答案是什麼。 – 2011-03-29 14:42:11

+0

簡短而親切。 – Nitin 2012-11-01 17:16:47

相關問題