Home > Software engineering >  Why does a WPF canvas result in System.InvalidOperationException?
Why does a WPF canvas result in System.InvalidOperationException?

Time:11-02

This code for a WPF works for one program. It is not working in a new program being created. It results in an exception --- "System.InvalidOperationException: 'Specified element is already the logical child of another element. Disconnect it first.'"

This is part of the .xaml code:

<Canvas x:Name="gCanvasPlotTop"
  HorizontalAlignment="Left"
  VerticalAlignment="Top"
  Margin="0,0,0,0"
  Width="500"
  Height="150" />

<Canvas x:Name="gCanvasPlotBottom" 
  HorizontalAlignment="Left" 
  VerticalAlignment="Top" 
  Margin="0,0,0,0" 
  Width="500"
  Height="150" />

Below is part of the .cs code.

Before I call dlgDisplayTwoXYPlots(), I define poXY:

Polyline poXYTop = new Polyline { Stroke = Brushes.Blue };
Polyline poXYBottom = new Polyline { Stroke = Brushes.Blue };

//------------------------------
public dlgDisplayTwoXYPlots(List<double> listdParamsTop,
                            List<Point> listPointsTop,
                            Polyline poXYTop,
                            List<double> listdParamsBottom,
                            List<Point> listPointsBottom,
                            Polyline poXYBottom)
{
  InitializeComponent();
  glistdParamsTop = listdParamsTop;
  glistPointsTop = listPointsTop;
  gpoXYTop = poXYTop;
  glistdParamsBottom = listdParamsBottom;
  glistPointsBottom = listPointsBottom;
  gpoXYBottom = poXYBottom;
}//DlgPlotXY()
//------------------------------
//------------------------------
private void Window_Loaded(object sender, RoutedEventArgs e)
{
  Plot(gCanvasPlotTop, gpoXYTop, glistdParamsTop, glistPointsTop);
  Plot(gCanvasPlotBottom, gpoXYBottom, glistdParamsBottom, glistPointsBottom);
}//Window_Loaded
//------------------------------
//------------------------------
private void Plot(Canvas canvas, Polyline poXY, List<double> listdParams, List<Point> listPoints)

{
  int iii = 0;
  int iNumOfPoints = (int)listdParams[iii  ];
  double dXmin = listdParams[iii  ];
  double dXmax = listdParams[iii  ];
  double dYmin = listdParams[iii  ];
  double dYmax = listdParams[iii  ];

  double dPlotWidth = dXmax - dXmin;
  double dPlotHeight = dYmax - dYmin;

  for (int ii = 0; ii < iNumOfPoints; ii  ) {
    var pointResult = new Point {
      X = (listPoints[ii].X - dXmin) * canvas.Width / dPlotWidth,
      Y = canvas.Height - (listPoints[ii].Y - dYmin) * canvas.Height / dPlotHeight
    };
    poXY.Points.Add(pointResult);
  }
  canvas.Children.Add(poXY);
//------------------------------

Why does a WPF canvas result in System.InvalidOperationException?

CodePudding user response:

In MainWindow I had

Polyline poXYTop = new Polyline { Stroke = Brushes.Blue};
Polyline poXYBottom = new Polyline { Stroke = Brushes.Blue};
OnePlot(listdSignalXValues, listdSignalYValues, poXYTop, "Signal");
TwoPlots(listdSignalXValues, listdSignalYValues, poXYTop, "Signal DupTop",
         listdSignalXValues, listdSignalYValues, poXYBottom, "Signal Bottom");

I had assumed that poXYTop would not return poXYTop with additional info as it was not a ref poXYTop. Now I know it does.

This works:

Polyline poXY = new Polyline { Stroke = Brushes.Blue };
Polyline poXYTop = new Polyline { Stroke = Brushes.Blue };
Polyline poXYBottom = new Polyline { Stroke = Brushes.Blue };
OnePlot(listdSignalXValues, listdSignalYValues, poXY, "Signal");
TwoPlots(listdSignalXValues, listdSignalYValues, poXYTop, "Signal DupTop",
         listdSignalXValues, listdSignalYValues, poXYBottom, "Signal Bottom");
  • Related