private DependencyObject GetNextCommandTargetElement(DependencyObject currentCommandTargetElement) { DependencyObject nextTargetElement = currentCommandTargetElement; DependencyObject temp; #if NET || NETCORE || NETFRAMEWORK if (currentCommandTargetElement is ContextMenu) { ContextMenu cm = currentCommandTargetElement as ContextMenu; temp = cm.PlacementTarget; } else #endif { temp = VisualTreeHelper.GetParent(currentCommandTargetElement); } if (temp == null) { FrameworkElement element = currentCommandTargetElement as FrameworkElement; if (element?.Parent == null) { nextTargetElement = CommonProperties.GetOwner(currentCommandTargetElement) as FrameworkElement; } else { nextTargetElement = element.Parent; } } else { nextTargetElement = temp; } return(nextTargetElement); }