public static object CheckedGetValue(this DependencyObject dependencyObject, DependencyProperty dependencyProperty) { dependencyProperty.AssertParameterNotNull("dependencyProperty"); return(dependencyObject. AssertNotNull(dependencyProperty.Name). GetValue(dependencyProperty)); }
public static void CheckedSetValue(this DependencyObject dependencyObject, DependencyProperty dependencyProperty, object value) { dependencyProperty.AssertParameterNotNull("dependencyProperty"); dependencyObject. AssertNotNull(dependencyProperty.Name). SetValue(dependencyProperty, value); }
public static DependencyObject GetVisualParent(this DependencyObject dependencyObject) { dependencyObject.AssertNotNull(nameof(dependencyObject)); return(dependencyObject.CaseType((Visual v) => VisualTreeHelper.GetParent(v)). CaseType((FrameworkContentElement f) => f.Parent). Default(o => throw new NotSupportedException($"DependencyObject.GetVisualParent() does not support type {o.GetType().Name}. \n " + $"The only supported types are {typeof(Visual).Name} and {typeof(FrameworkContentElement).Name}.")). Result); }
public static void SetBinding(this DependencyObject target, DependencyProperty property, object source, string sourcePath) { target.AssertNotNull(nameof(target)); BindingOperations.SetBinding(target, property, new Binding(sourcePath) { Source = source }); }
public static IEnumerable <DependencyObject> TraverseUpVisual(this DependencyObject self) { self.AssertNotNull(nameof(self)); while (self != null) { yield return(self); self = VisualTreeHelper.GetParent(self); } }
public static IEnumerable <DependencyObject> GetVisualChildren(this DependencyObject self) { self.AssertNotNull(nameof(self)); var childCount = VisualTreeHelper.GetChildrenCount(self); for (var i = 0; i < childCount; i++) { yield return(VisualTreeHelper.GetChild(self, i)); } }
public static IEnumerable <DependencyObject> GetVisualChildren(this DependencyObject dependencyObject, Predicate <DependencyObject> predicate = null) { dependencyObject.AssertNotNull(nameof(dependencyObject)); predicate = predicate ?? (o => true); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++) { var visualChild = dependencyObject.GetVisualChild(i); if (predicate(visualChild)) { yield return(dependencyObject.GetVisualChild(i)); } } }
public static void SetBinding(this DependencyObject target, DependencyProperty property, string sourcePath, IValueConverter converter, object converterParam = null, object source = null) { target.AssertNotNull(nameof(target)); var binding = new Binding(sourcePath) { Converter = converter, ConverterParameter = converterParam, Source = source }; BindingOperations.SetBinding(target, property, binding); }
public static void BindConverter(this DependencyObject target, DependencyProperty property, IValueConverter converter, object sourceValue) { target.AssertNotNull(nameof(target)); var binding = new Binding { Converter = converter, Source = sourceValue, BindsDirectlyToSource = true }; BindingOperations.SetBinding(target, property, binding); }
public static IEnumerable <DependencyObject> GetVisualAncestors(this DependencyObject dependencyObject, Predicate <DependencyObject> predicate = null) { dependencyObject.AssertNotNull(nameof(dependencyObject)); predicate = predicate ?? (o => true); DependencyObject parent = dependencyObject.GetVisualParent(); while (parent != null) { if (predicate(parent)) { yield return(parent); } parent = parent.GetVisualParent(); } }
public static DependencyObject FindVisualParent(this DependencyObject dependencyObject, Predicate <DependencyObject> predicate) { dependencyObject.AssertNotNull(nameof(dependencyObject)); predicate.AssertParameterNotNull(nameof(predicate)); var parent = dependencyObject.GetVisualParent(); while (parent != null) { if (predicate(parent)) { return(parent); } parent = parent.GetVisualParent(); } return(null); }
public static IEnumerable <DependencyObject> GetVisualDescendants(this DependencyObject dependencyObject, Predicate <DependencyObject> predicate = null) { dependencyObject.AssertNotNull(nameof(dependencyObject)); predicate = predicate ?? (o => true); var result = new List <DependencyObject>(); foreach (var child in dependencyObject.GetVisualChildren()) { if (predicate(child)) { result.Add(child); } result.AddRange(child.GetVisualDescendants(predicate)); } return(result); }
public static bool SetIsSealed(this DependencyObject self, bool isSealed) { self.AssertNotNull(nameof(self)); if (self.IsSealed == isSealed) { return(true); } var sealProperty = typeof(DependencyObject).GetProperty("DO_Sealed", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance | BindingFlags.DeclaredOnly); if (sealProperty == null) { return(false); } else { sealProperty.SetValue(self, isSealed, null); return(true); } }
public static bool IsVisualChildOf(this DependencyObject dependencyObject, DependencyObject ancestor) { dependencyObject.AssertNotNull(nameof(dependencyObject)); ancestor.AssertParameterNotNull(nameof(ancestor)); var parent = dependencyObject; while (true) { parent = parent.GetVisualParent(); if (parent == ancestor) { return(true); } if (parent == null) { return(false); } } }
public static IEnumerable <TChild> GetVisualChildrenOfType <TChild>(this DependencyObject dependencyObject) where TChild : DependencyObject { dependencyObject.AssertNotNull(nameof(dependencyObject)); return(dependencyObject.GetVisualChildren(o => (o is TChild)).Cast <TChild>()); }
public static IEnumerable <TObject> GetVisualAncestorsOfType <TObject>(this DependencyObject dependencyObject) where TObject : DependencyObject { dependencyObject.AssertNotNull(nameof(dependencyObject)); return(dependencyObject.GetVisualAncestors(o => (o is TObject)).Cast <TObject>()); }
public static TObject FindVisualParentOfType <TObject>(this DependencyObject dependencyObject) where TObject : DependencyObject { dependencyObject.AssertNotNull(nameof(dependencyObject)); return(dependencyObject.FindVisualParent(o => (o is TObject)) as TObject); }
private static void SetIsDragInProgress(DependencyObject dependencyObject, bool isDragInProgress) { dependencyObject.AssertNotNull("dependencyObject"); dependencyObject.SetValue(isDragInProgressPropertyKey, isDragInProgress); }
public static bool GetIsDragInProgress(DependencyObject dependencyObject) { dependencyObject.AssertNotNull("dependencyObject"); return((bool)dependencyObject.GetValue(IsDragInProgressProperty)); }
public static void SetDragParent(DependencyObject dependencyObject, FrameworkElement dragParent) { dependencyObject.AssertNotNull("dependencyObject"); dependencyObject.SetValue(DragParentProperty, dragParent); }
public static FrameworkElement GetDragParent(DependencyObject dependencyObject) { dependencyObject.AssertNotNull("dependencyObject"); return(dependencyObject.GetValue(DragParentProperty) as FrameworkElement); }
public static DependencyObject GetVisualChild(this DependencyObject dependencyObject, int childIndex) { dependencyObject.AssertNotNull(nameof(dependencyObject)); return(VisualTreeHelper.GetChild(dependencyObject, childIndex)); }
public static IEnumerable <DependencyObject> GetVisualChildrenDepthFirst(this DependencyObject self) { self.AssertNotNull(nameof(self)); return(GetVisualChildren(self).FlattenTreeDepthFirst(GetVisualChildren)); }