diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Extensions/DependencyObject.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,25 @@
+using System.Windows;
+using System.Windows.Media;
+
+namespace MetroWpf.Xaml.Extensions
+{
+  public static class DependencyObjectExtensions
+  { 
+    /// <summary>
+    /// Finds a parent of a given control/item on the visual tree.
+    /// </summary>
+    /// <typeparam name="T">Type of Parent</typeparam>
+    /// <param name="child">Child whose parent is queried</param>
+    /// <returns>Returns the first parent item that matched the type (T), if no match found then it will return null</returns>
+    public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
+    {
+      DependencyObject parentObject = VisualTreeHelper.GetParent(child);
+      if (parentObject == null) return null;
+      T parent = parentObject as T;
+      if (parent != null)
+        return parent;
+      else
+        return TryFindParent<T>(parentObject);
+    }  
+  }
+}