comparison MetroWpf/MetroWpf.Xaml/Extensions/DependencyObject.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System.Windows;
2 using System.Windows.Media;
3
4 namespace MetroWpf.Xaml.Extensions
5 {
6 public static class DependencyObjectExtensions
7 {
8 /// <summary>
9 /// Finds a parent of a given control/item on the visual tree.
10 /// </summary>
11 /// <typeparam name="T">Type of Parent</typeparam>
12 /// <param name="child">Child whose parent is queried</param>
13 /// <returns>Returns the first parent item that matched the type (T), if no match found then it will return null</returns>
14 public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
15 {
16 DependencyObject parentObject = VisualTreeHelper.GetParent(child);
17 if (parentObject == null) return null;
18 T parent = parentObject as T;
19 if (parent != null)
20 return parent;
21 else
22 return TryFindParent<T>(parentObject);
23 }
24 }
25 }