public static T?FindParent <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(parentObject.FindParent <T>()); } }
private static void OnFocusOnLoadChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var parentTreeView = d.FindParent<TreeListView>(); if (parentTreeView == null || !parentTreeView.IsFocusOnLoadEnabled) return; var frameworkElement = d as FrameworkElement; if (frameworkElement != null) { if (frameworkElement.IsLoaded) { Keyboard.Focus(frameworkElement); } else { frameworkElement.Loaded += FrameworkElement_Loaded; } } }
static DependencyObject FindDragLeaveCommandHolder( DependencyObject originalSource ) { var element = originalSource.FindParent<DependencyObject>( t => { return DragDropManager.GetOnDragLeaveCommand( t ) != null; } ); return element; }
/// <summary> /// Finds the WPF element where the DropTarget property has been defined. /// </summary> /// <param name="originalSource"> /// The original source where the Drop, /// or DragOver, operation is happening. /// </param> /// <returns> /// The element that holds the DropTarget, /// or null if none can be found. /// </returns> static DependencyObject FindDropTargetContainer( DependencyObject originalSource ) { var element = originalSource.FindParent<DependencyObject>( t => { return DragDropManager.GetDropTarget( t ) != null; } ); return element; }