views:

65

answers:

1

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:

http://efreedom.com/Question/1-2837293/Error-Using-TransformToAncestor-Specified-Visual-Ancestor-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);
        }