using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Animation; /// /// Contains a collection of extension methods. /// public static class DependencyObjectExtensions { /// /// Invokes the current dependency object dispatcher. /// /// The dependency object. /// The action. public static void BeginInvoke(this DependencyObject dependencyObject, Action action) { dependencyObject.Dispatcher.BeginInvoke(action); } /// /// Invokes the specified action. /// /// The dependency object. /// The action. public static void Invoke(this DependencyObject dependencyObject, Action action) { dependencyObject.Dispatcher.Invoke(action); } /// /// Determines whether this object is currently in design time mode. /// /// The object. /// public static bool IsInDesignMode(this DependencyObject obj) { return (DesignerProperties.GetIsInDesignMode(obj)); } /// /// Binds the specified dependency property to a another object property. /// /// The target dependency object. /// The target dependency property. /// The source object. /// The source dependency property. /// Binding mode. /// Binding converter. /// public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode, IValueConverter converter) { Binding binding = new Binding(); binding.Mode = mode; binding.Source = source; binding.Path = new PropertyPath(sourceDP); binding.Converter = converter; BindingOperations.SetBinding(target, targetDP, binding); return binding; } /// /// Binds the specified dependency property to a another object property. /// /// The target dependency object. /// The target dependency property. /// The source object. /// The source dependency property. /// Binding mode. /// public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode = BindingMode.Default) { Binding binding = new Binding(); binding.Mode = mode; binding.Source = source; binding.Path = new PropertyPath(sourceDP); BindingOperations.SetBinding(target, targetDP, binding); return binding; } /// /// Create asynchronous binding between target and source. /// /// The target dependency object. /// The target dependency property. /// The source object. /// The source dependency property. /// Binding mode. /// public static Binding BindAsync(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode = BindingMode.Default) { Binding binding = new Binding(); binding.Mode = mode; binding.Source = source; binding.Path = new PropertyPath(sourceDP); binding.IsAsync = true; binding.BindsDirectlyToSource = true; BindingOperations.SetBinding(target, targetDP, binding); return binding; } /// /// Binds the specified dependency property to a another object property. /// /// The target dependency object. /// The target dependency property. /// The source object. /// The source dependency property name. /// Binding mode. /// public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, String sourceDP, BindingMode mode = BindingMode.Default) { Binding binding = new Binding(); binding.Mode = mode; binding.Source = source; binding.Path = new PropertyPath(sourceDP); BindingOperations.SetBinding(target, targetDP, binding); return binding; } /// /// Binds the specified dependency property to a another object property. /// /// The target dependency object. /// The target dependency property. /// The source object. /// The source dependency property name. /// Binding mode. /// public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, Object source, String sourcePath, BindingMode mode = BindingMode.Default) { Binding binding = new Binding(); binding.Mode = mode; binding.Source = source; binding.Path = new PropertyPath(sourcePath); BindingOperations.SetBinding(target, targetDP, binding); return binding; } /// /// Clears bindings from the specified dependency property. /// /// The target dependency object. /// The dependency property. public static void Unbind(this DependencyObject target, DependencyProperty dependencyProperty) { BindingOperations.ClearBinding(target, dependencyProperty); } /// /// Finds the ancestor of type T. /// /// /// The dependency object. /// public static T FindAncestor(this DependencyObject dependencyObject) where T : class { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindAncestor(parent); } /// /// Finds an ancestor that matches the specified criteria. /// /// The dependency object. /// The predicate. /// public static DependencyObject FindAncestor(this DependencyObject dependencyObject, Predicate predicate) { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; if (predicate(parent)) { return parent; } else { return FindAncestor(parent, predicate); } } /// /// Finds the child of type T. /// /// /// The dependency object. /// public static T FindChild(this DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? FindChild(child); if (result != null) return result; } return null; } /// /// Finds a Child of a given item in the visual tree. /// /// A direct parent of the queried item. /// The type of the queried item. /// x:Name or Name of child. /// The first parent item that matches the submitted type parameter. /// If not matching item can be found, /// a null parent is being returned. public static T FindChild(this DependencyObject parent, string childName) where T : DependencyObject { // Confirm parent and childName are valid. if (parent == null) return null; T foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); // If the child is not of the request child type child T childType = child as T; if (childType == null) { // recursively drill down the tree foundChild = FindChild(child, childName); // If the child is found, break so we do not overwrite the found child. if (foundChild != null) break; } else if (!string.IsNullOrEmpty(childName)) { var frameworkElement = child as FrameworkElement; // If the child's name is set for search if (frameworkElement != null && frameworkElement.Name == childName) { // if the child's name is of the request name foundChild = (T)child; break; } } else { // child element found. foundChild = (T)child; break; } } return foundChild; } /// /// Finds the visual children of type T. /// /// type of visual /// The dependency object. /// public static IEnumerable FindVisualChildren(this DependencyObject depObj) 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) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren(child)) { yield return childOfChild; } } } } }