public static T FindParent <T>(DependencyObject child) where T : DependencyObject { if (!(child is Visual)) { return(null); } DependencyObject parent = VisualTreeHelper.GetParent(child) as DependencyObject; if (parent == null) { return(null); } if (parent is T) { return(parent as T); } return(VisualTreeExtensions.FindParent <T>(parent)); }
public static T FindParentByName <T>(DependencyObject child, string name) where T : DependencyObject { DependencyObject parent = VisualTreeHelper.GetParent(child) as DependencyObject; if (parent == null) { return(null); } FrameworkElement fe = parent as FrameworkElement; if (fe != null) { if (fe is T && fe.Name.CompareTo(name) == 0) { return(parent as T); } } return(VisualTreeExtensions.FindParent <T>(parent)); }
// A common way to implement an adorner's rendering behavior is to override the OnRender // method, which is called by the layout system as part of a rendering pass. protected override void OnRender(DrawingContext drawingContext) { Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize); // Some arbitrary drawing implements. SolidColorBrush renderBrush = new SolidColorBrush(Colors.White); //renderBrush.Opacity = 0.2; double renderRadius = 5.0; // Draw a circle at each corner. Viewbox parentViewbox = VisualTreeExtensions.FindParent <Viewbox>(this.AdornedElement); double factor = parentViewbox.Child.RenderSize.Width / parentViewbox.RenderSize.Width; renderRadius *= factor; Pen renderPen = new Pen(Brushes.Black, 1.5 * factor); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius); }