// Helper method used from GetNeighborSelectionStop() // Returns a parent DependencyObject of the specified element that is // // * Visible AND // * ( Marked with a SelectionStop OR // * Marked with IsSelectionScope = true OR // * Has more than one child ) // private static DependencyObject GetEligibleParent(DependencyObject element, out int childIndex, out int childrenCount, out int childDepth, out bool isSelectionStop, out bool isSelectionScope) { childDepth = 0; isSelectionStop = false; isSelectionScope = false; bool isVisible; do { element = VisualTreeUtils.GetIndexedVisualParent(element, out childrenCount, out childIndex); isSelectionStop = element == null ? false : (GetSelectionStop(element) != null); isSelectionScope = element == null ? false : GetIsSelectionScope(element); isVisible = VisualTreeUtils.IsVisible(element as UIElement); childDepth++; }while ( element != null && (isVisible == false || (isSelectionStop == false && isSelectionScope == false && childrenCount < 2))); return(element); }