"The specified Visual is not an ancestor of this Visual" exception in OxySKElement.OnRender() · Issue #1787 · oxyplot/oxyplot · GitHub
Skip to content

"The specified Visual is not an ancestor of this Visual" exception in OxySKElement.OnRender()  #1787

Closed

Description

@albtaeler

Description

We use a docking manager in our application. With it it's possible to dock out windows with plots and arrange them on another screens.
With the current oxyplot version and OxyPlot.SkiaSharp.Wpf.PlotView, we get an exception after undocking a window with a plot.

Partial stack-trace:

System.InvalidOperationException
  HResult=0x80131509
  Message=The specified Visual is not an ancestor of this Visual.
  Source=PresentationCore
  StackTrace:
   at System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix& simpleTransform)
   at System.Windows.Media.Visual.TransformToAncestor(Visual ancestor)
   at OxyPlot.SkiaSharp.Wpf.OxySKElement.OnRender(DrawingContext drawingContext)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
    ...
    [Call Stack Truncated]

  This exception was originally thrown at this call stack:
    System.Windows.Media.Visual.TrySimpleTransformToAncestor(System.Windows.Media.Visual, bool, out System.Windows.Media.GeneralTransform, out System.Windows.Media.Matrix)
    System.Windows.Media.Visual.TransformToAncestor(System.Windows.Media.Visual)
    OxyPlot.SkiaSharp.Wpf.OxySKElement.OnRender(System.Windows.Media.DrawingContext)
    System.Windows.UIElement.Arrange(System.Windows.Rect)
    System.Windows.Controls.Grid.ArrangeOverride(System.Windows.Size)
    System.Windows.FrameworkElement.ArrangeCore(System.Windows.Rect)
    System.Windows.UIElement.Arrange(System.Windows.Rect)
    System.Windows.Controls.Border.ArrangeOverride(System.Windows.Size)
    System.Windows.FrameworkElement.ArrangeCore(System.Windows.Rect)
    System.Windows.UIElement.Arrange(System.Windows.Rect)
    ...
    [Call Stack Truncated]

Steps to reproduce

  1. Use a docking manager like syncfusion DockingManger
  2. Undock the window with the plot, so that the window with the PlotView is no longer a descendant of the MainWindow in the VisualTree .
    VisualTree of the Application with floating window:
    image

Platform: x86 & x64
.NET version: 4.7.2
Latest OxyPlot Version (nuget packages build from develop brunch)

Actual behaviour

The call Window.GetWindow(this) in OxySKElement.OnRender() returns always the MainWindow. I think it uses the logical tree not the visual tree and this.TransformToAncestor() fails, because the MainWindow is actually not an ancestor of the plot in case of a floating window.

Possible Fix

Get the ancestor from the visual tree with a methode like this

private Window GetAncestorWindowFromVisualTree(DependencyObject startElement)
        {
            DependencyObject parent = startElement;
            while (!(parent is Window))
            {
                if (parent == null) { break; }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return parent as Window ?? Window.GetWindow(this);
        }

var visualOffset = this.TransformToAncestor(GetAncestorWindowFromVisualTree(this)).Transform(default);

Activity

  1. VisualMelon commented on Sep 8, 2021

    @VisualMelon
    Contributor
  2. albtaeler commented on Sep 8, 2021

    @albtaeler
    ContributorAuthor

    Yes, that's right. I've tested it, it has the same issue.
    We've updated from a quite old version.
    Version f9cebf3 from 2019-01-31 didn't have this issue.

  3. VisualMelon commented on Sep 8, 2021

    @VisualMelon
    Contributor

    Thanks for reporting and checking that. It's been a while since I had to understand WPF, but I'll try to get my head around this.

    If you wanted to submit a PR, I'd be happy to review it; otherwise, I'll try to sort one out over the weekend.

    This is presumably then a significant regression with 2.1 which we should address before publishing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions