Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************************
- // example use:
- //
- // var tb = null as TextBox;
- // tb = FindVisualChild<TextBox>(container as DependencyObject, "tbTrigger");
- //***********************************
- public static T FindVisualChild<T>(DependencyObject depObj, string objectName) 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)
- {
- var name = child.ReadLocalValue(NameProperty);
- if (name.ToString() == objectName)
- return (T)child;
- }
- T childItem = FindVisualChild<T>(child, objectName);
- if (childItem != null) return childItem;
- }
- }
- return null;
- }
- public static T FindVisualParent<T>(DependencyObject depObj) where T : DependencyObject
- {
- bool IsParentFound = false;
- DependencyObject parent;
- if (depObj != null)
- {
- while (!IsParentFound)
- {
- parent = VisualTreeHelper.GetParent(depObj);
- if (parent != null && parent is T)
- {
- IsParentFound = true;
- return (T)parent;
- }
- T parentItem = FindVisualParent<T>(parent);
- if (parentItem != null) return parentItem;
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement