异常:指定的 Visual 不是此 Visual 的祖先

发布于 2024-09-29 16:49:10 字数 4432 浏览 0 评论 0原文

以下方法在该行中抛出异常:

Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));

但是如果您查看代码,vb 肯定是 surfaceWindow 的子级。那么为什么这不起作用呢?

if (!isExpanded())
            {
                Viewbox vb = new Viewbox();

                ClassMetricView metricView = new ClassMetricView();
                metricView.Width = 300;
                metricView.Height = 300;
                metricView.ClassName = this.name;
                metricView.NumberOfMetrics = 6;
                metricView.LOC = this.getLoc();
                metricView.FanIn = this.getFanIn();
                metricView.FanOut = this.getFanOut();
                metricView.buildComponent();
                vb.Child = metricView;
                vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

                surfaceWindow.ClassScatter.Items.Add(vb);
                this.setExpanded(true);

                //Create line to connect these UI elements

                Point parentPosition = surfaceWindow.RootContainer.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
                Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
                Line line = new Line();
                line.X1 = parentPosition.X;
                line.Y1 = parentPosition.Y;
                line.X2 = childPosition.X;
                line.Y2 = childPosition.Y;
                line.Stroke = System.Windows.Media.Brushes.Black;
                line.StrokeThickness = 2;
                surfaceWindow.RootGrid.Children.Add(line);
            }

编辑:我找到了问题的答案:

使用 TransformToAncestor 时出错:“指定的视觉对象不是此视觉对象的祖先。”

问题是,我不明白解决方案。谁能解释一下吗?

编辑2:我尝试实现这个调度程序。但仍然抛出相同的异常。任何提示都会很棒!

 public void expand(SurfaceWindow1 surfaceWindow)
        {
            _surfaceWindow = surfaceWindow;
            Logging.Logger.getInstance().log("Expand class " + name);

            if (!isExpanded())
            {
                Viewbox vb = new Viewbox();

                ClassMetricView metricView = new ClassMetricView();
                metricView.Width = 300;
                metricView.Height = 300;
                metricView.ClassName = this.name;
                metricView.NumberOfMetrics = 5;
                metricView.NumberOfRevisions = 6;
                metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe"};
                int[,] values = { { 10, 10, 10, 10, 10}, {20, 20, 20, 20, 20}, {30, 30, 30, 30, 30}, {40, 40, 40, 40, 40}, {50, 50, 50, 50, 50}, {60, 60, 60, 60, 60} };
                metricView.Metrics = values;
                metricView.buildComponent();
                vb.Child = metricView;
                vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

                surfaceWindow.ClassScatter.Items.Add(vb);
                this.setExpanded(true);

                //Create line to connect these UI elements
                System.Threading.Thread thread = new System.Threading.Thread(
    new System.Threading.ThreadStart(
      delegate()
      {
          vb.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
              delegate()
              {
                  SetStatus(vb);
              }
          ));
      }
  ));

                thread.Start();
            }
        }

        private void SetStatus(Viewbox vb)
        {

            Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
            Point childPosition = vb.TransformToAncestor(Window.GetWindow(vb)).Transform(new Point(0, 0));
            //Point childPosition = new Point(0, 0);

            Line line = new Line();
            line.X1 = parentPosition.X;
            line.Y1 = parentPosition.Y;
            line.X2 = childPosition.X;
            line.Y2 = childPosition.Y;
            line.Stroke = System.Windows.Media.Brushes.Black;
            line.StrokeThickness = 2;
            _surfaceWindow.RootGrid.Children.Add(line);

            Console.WriteLine("Draw line with position: " + line.X1 + "/" + line.Y1 + "/" + line.X2 + "/" + line.Y2);
        }

The following method thrwos an exception in the line:

Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));

But if you look at the code, the vb is surely a child of surfaceWindow. So why isn't this working?

if (!isExpanded())
            {
                Viewbox vb = new Viewbox();

                ClassMetricView metricView = new ClassMetricView();
                metricView.Width = 300;
                metricView.Height = 300;
                metricView.ClassName = this.name;
                metricView.NumberOfMetrics = 6;
                metricView.LOC = this.getLoc();
                metricView.FanIn = this.getFanIn();
                metricView.FanOut = this.getFanOut();
                metricView.buildComponent();
                vb.Child = metricView;
                vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

                surfaceWindow.ClassScatter.Items.Add(vb);
                this.setExpanded(true);

                //Create line to connect these UI elements

                Point parentPosition = surfaceWindow.RootContainer.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
                Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
                Line line = new Line();
                line.X1 = parentPosition.X;
                line.Y1 = parentPosition.Y;
                line.X2 = childPosition.X;
                line.Y2 = childPosition.Y;
                line.Stroke = System.Windows.Media.Brushes.Black;
                line.StrokeThickness = 2;
                surfaceWindow.RootGrid.Children.Add(line);
            }

EDIT: I found maybe an answer to my problem:

Error when using TransformToAncestor: "The specified Visual is not an ancestor of this Visual."

the problem is, I don't understand the solution. Can anyone explain?

EDIT 2: I tried to implement this dispatcher. But still the same exception is thrown. Any hints would be really great!

 public void expand(SurfaceWindow1 surfaceWindow)
        {
            _surfaceWindow = surfaceWindow;
            Logging.Logger.getInstance().log("Expand class " + name);

            if (!isExpanded())
            {
                Viewbox vb = new Viewbox();

                ClassMetricView metricView = new ClassMetricView();
                metricView.Width = 300;
                metricView.Height = 300;
                metricView.ClassName = this.name;
                metricView.NumberOfMetrics = 5;
                metricView.NumberOfRevisions = 6;
                metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe"};
                int[,] values = { { 10, 10, 10, 10, 10}, {20, 20, 20, 20, 20}, {30, 30, 30, 30, 30}, {40, 40, 40, 40, 40}, {50, 50, 50, 50, 50}, {60, 60, 60, 60, 60} };
                metricView.Metrics = values;
                metricView.buildComponent();
                vb.Child = metricView;
                vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

                surfaceWindow.ClassScatter.Items.Add(vb);
                this.setExpanded(true);

                //Create line to connect these UI elements
                System.Threading.Thread thread = new System.Threading.Thread(
    new System.Threading.ThreadStart(
      delegate()
      {
          vb.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
              delegate()
              {
                  SetStatus(vb);
              }
          ));
      }
  ));

                thread.Start();
            }
        }

        private void SetStatus(Viewbox vb)
        {

            Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
            Point childPosition = vb.TransformToAncestor(Window.GetWindow(vb)).Transform(new Point(0, 0));
            //Point childPosition = new Point(0, 0);

            Line line = new Line();
            line.X1 = parentPosition.X;
            line.Y1 = parentPosition.Y;
            line.X2 = childPosition.X;
            line.Y2 = childPosition.Y;
            line.Stroke = System.Windows.Media.Brushes.Black;
            line.StrokeThickness = 2;
            _surfaceWindow.RootGrid.Children.Add(line);

            Console.WriteLine("Draw line with position: " + line.X1 + "/" + line.Y1 + "/" + line.X2 + "/" + line.Y2);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-10-06 16:49:10

我终于能够解决这个问题了。看来 ScatterViewItem 在调用后尚未添加到 VisualTree 中,

surfaceWindow.ClassScatter.Item.Add(vb);

因此我向 ScatterViewItem 添加了 SizeChangedEventHandler 和 ScatterManipulationDeltaEventHandler 并在其中添加了行。

感谢 Bart,他帮助我解决了这个

if (!isExpanded())
{
    Viewbox vb = new Viewbox();

    ClassMetricView metricView = new ClassMetricView();
    metricView.Width = 300;
    metricView.Height = 300;
    metricView.ClassName = this.name;
    metricView.NumberOfMetrics = 5;
    metricView.NumberOfRevisions = 6;
    metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe" };
    int[,] values = { { 10, 10, 10, 10, 10 }, { 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30 }, { 40, 40, 40, 40, 40 }, { 50, 50, 50, 50, 50 }, { 60, 60, 60, 60, 60 } };
    metricView.Metrics = values;
    metricView.buildComponent();
    vb.Child = metricView;
    vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

    surfaceWindow.ClassScatter.Items.Add(vb);

    ScatterViewItem svItem = _surfaceWindow.ClassScatter.ItemContainerGenerator.ContainerFromItem(vb) as ScatterViewItem;
    svItem.Tag = this.name;
    svItem.AddHandler(ScatterViewItem.ScatterManipulationDeltaEvent, new ScatterManipulationDeltaEventHandler(MovementHandler));
    svItem.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(ScatterSizeChanged));

    this.setExpanded(true);
}

问题

public void MovementHandler(object sender, ScatterManipulationDeltaEventArgs e)
{
    updateConnectingLines(sender);
}

public void ScatterSizeChanged(object sender, SizeChangedEventArgs e)
{
    updateConnectingLines(sender);
}

private void updateConnectingLines(object sender)
{
    removeOldLines((sender as ScatterViewItem).Tag as String);

    Point childPosition = (sender as ScatterViewItem).TransformToAncestor(_surfaceWindow.ClassScatter).Transform(new Point(0, 0));
    Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));

    Line line = new Line();
    line.X1 = parentPosition.X;
    line.Y1 = parentPosition.Y;
    line.X2 = childPosition.X;
    line.Y2 = childPosition.Y;
    line.Stroke = System.Windows.Media.Brushes.Black;
    line.StrokeThickness = 2;
    line.Tag = this.name;
    lines.Add(line);

    _surfaceWindow.RootGrid.Children.Add(line);
}

I was finally able to solve the problem. It seems true that the ScatterViewItem is not yet added to the VisualTree right after the call

surfaceWindow.ClassScatter.Item.Add(vb);

So I added a SizeChangedEventHandler and a ScatterManipulationDeltaEventHandler to the ScatterViewItem and add the lines there.

Thanks to Bart, who helped me with this solution

if (!isExpanded())
{
    Viewbox vb = new Viewbox();

    ClassMetricView metricView = new ClassMetricView();
    metricView.Width = 300;
    metricView.Height = 300;
    metricView.ClassName = this.name;
    metricView.NumberOfMetrics = 5;
    metricView.NumberOfRevisions = 6;
    metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe" };
    int[,] values = { { 10, 10, 10, 10, 10 }, { 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30 }, { 40, 40, 40, 40, 40 }, { 50, 50, 50, 50, 50 }, { 60, 60, 60, 60, 60 } };
    metricView.Metrics = values;
    metricView.buildComponent();
    vb.Child = metricView;
    vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));

    surfaceWindow.ClassScatter.Items.Add(vb);

    ScatterViewItem svItem = _surfaceWindow.ClassScatter.ItemContainerGenerator.ContainerFromItem(vb) as ScatterViewItem;
    svItem.Tag = this.name;
    svItem.AddHandler(ScatterViewItem.ScatterManipulationDeltaEvent, new ScatterManipulationDeltaEventHandler(MovementHandler));
    svItem.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(ScatterSizeChanged));

    this.setExpanded(true);
}

and

public void MovementHandler(object sender, ScatterManipulationDeltaEventArgs e)
{
    updateConnectingLines(sender);
}

public void ScatterSizeChanged(object sender, SizeChangedEventArgs e)
{
    updateConnectingLines(sender);
}

private void updateConnectingLines(object sender)
{
    removeOldLines((sender as ScatterViewItem).Tag as String);

    Point childPosition = (sender as ScatterViewItem).TransformToAncestor(_surfaceWindow.ClassScatter).Transform(new Point(0, 0));
    Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));

    Line line = new Line();
    line.X1 = parentPosition.X;
    line.Y1 = parentPosition.Y;
    line.X2 = childPosition.X;
    line.Y2 = childPosition.Y;
    line.Stroke = System.Windows.Media.Brushes.Black;
    line.StrokeThickness = 2;
    line.Tag = this.name;
    lines.Add(line);

    _surfaceWindow.RootGrid.Children.Add(line);
}
一曲琵琶半遮面シ 2024-10-06 16:49:10

我在将项目列表分配给 DataGrid 时遇到了同样的错误。事实证明,如果列表为空,即使列表不为空。

要解决这个问题,添加验证就足够了:

if (listOfObjectsToShowInTheGrid?.Any() ?? false)
{
    this.DataGrid1.ItemsSource = listOfObjectsToShowInTheGrid;
    this.DataGrid1.UpdateLayout();
}
else
{
     // Do somethig if the list is empty
}

I encountered the same error while assigning a list of items to a DataGrid. Turned out that if the list was empty, even though the list wasn't null.

To solve that, adding a validation is enough:

if (listOfObjectsToShowInTheGrid?.Any() ?? false)
{
    this.DataGrid1.ItemsSource = listOfObjectsToShowInTheGrid;
    this.DataGrid1.UpdateLayout();
}
else
{
     // Do somethig if the list is empty
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文