Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get all controls in wpf window and processing it?
Posted
Comments
bbirajdar 30-Oct-13 6:34am    
this.Form.Controls
Thomas Daniels 30-Oct-13 6:35am    
That's in Windows Forms, but the question is about WPF.

This should do the trick

XML
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;
            }
        }
    }
}


then you enumerate over the controls like so
XML
foreach (TextBlock tb in FindVisualChildren<TextBlock>(window))
{
    // do something with tb here
}
 
Share this answer
 
You can use this Solution:

C#
private void HideAllControl()
{
  /// casting the content into panel
  Panel mainContainer = (Panel)this.Content;

  /// GetAll UIElement
  UIElementCollection element = mainContainer.Children;

  /// casting the UIElementCollection into List
  List < FrameworkElement> lstElement = element.Cast<FrameworkElement> ().ToList();

  /// Geting all Control from list
  var lstControl = lstElement.OfType<Control>();

  foreach (Control contol in lstControl)
  {
   ///Hide all Controls
   contol.Visibility = System.Windows.Visibility.Hidden;
  }
}
 
Share this answer
 
v4
For such purposes, this is the specific part of WPF you need to learn: http://msdn.microsoft.com/en-us/library/ms753391.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900