Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


CSharp Programming

CSharp > Code Examples

Find Control With Tag

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Find Control With Tag using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Media; static class common { public static T FindControlWithTag<T>(this DependencyObject parent, string tag) where T : UIElement { List<UIElement> elements = new List<UIElement>(); int count = VisualTreeHelper.GetChildrenCount(parent); if (count > 0) { for (int j = 0; j < count; j++) { DependencyObject child = VisualTreeHelper.GetChild(parent, j); if (typeof(FrameworkElement).IsAssignableFrom(child.GetType()) && ((string)((FrameworkElement)child).Tag == tag)) { return child as T; } var item = FindControlWithTag<T>(child, tag); if (item != null) return item as T; } } return null; } }