Find children : DependencyProperty « Windows Presentation Foundation « C# / C Sharp






Find children

        

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


public class UIHelper
{
    private static IEnumerable<T> FindChildren<T>(DependencyObject parent) where T : class
    {
        var count = VisualTreeHelper.GetChildrenCount(parent);
        if (count > 0)
        {
            for (var i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                var t = child as T;
                if (t != null)
                    yield return t;

                var children = FindChildren<T>(child);
                foreach (var item in children)
                    yield return item;
            }
        }
    }

    public static void SetDataContext<T>(DependencyObject parent, T dataContext) where T : class
    {
        var textBoxes = FindChildren<TextBox>(parent);
        foreach (var item in textBoxes)
        {
            item.DataContext = dataContext;
        }
    }

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.DependencyProperty.RegisterReadOnly to create read only Dependency PropertyDependencyProperty.RegisterReadOnly to create read only Dependency Property
2.Clear locally set values and restore the default values of dependency propertiesClear locally set values and restore the default values of dependency properties
3.Get Children for DependencyObject
4.Find Control With Tag