locked
set canvas child in windows store application

    Question

  • i have created two canvas in xaml like this :

    <Grid>
    <Canvas x:Name="canvas2"    Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="11" Grid.RowSpan="8" Background="White"  >
    </Canvas>
    <Canvas x:Name="canvas1" RightTapped="canvas1_RightTapped"  Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="11" Grid.RowSpan="8" Background="Transparent"  >
    </Canvas>
    </Grid>

    now i want to make these canvases children of another canvas that should be create on button click . I am doing it like this :

    Canvas canvas1d = new Canvas();
    canvas1d.Height = canvas1.ActualHeight; 
    canvas1d.Width = canvas1.ActualWidth; 
    canvas1d = canvas1;
    Canvas canvas2d = new Canvas();
    canvas2d.Height = canvas1.ActualHeight;
    canvas2d.Width = canvas1.ActualWidth;
    canvas2d = canvas2;
    Canvas canvas = new Canvas();
    canvas.Height = canvas1.ActualHeight; 
    canvas.Width = canvas1.ActualWidth;
    canvas.Children.Add(canvas2d);
    canvas.Children.Add(canvas1d);

    but it is given an error

    An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code.Element is already the child of another element

    please help and correct this code.

    Sunday, March 23, 2014 10:31 AM

Answers

  • I do understand that.   This code should work.  

    Canvas canvas1d = new Canvas();
    canvas1d.Height = canvas1.ActualHeight; 
    canvas1d.Width = canvas1.ActualWidth; 
    
    Canvas canvas2d = new Canvas();
    canvas2d.Height = canvas1.ActualHeight;
    canvas2d.Width = canvas1.ActualWidth;
    
    Canvas canvas = new Canvas();
    canvas.Height = canvas1.ActualHeight; 
    canvas.Width = canvas1.ActualWidth;
    canvas.Children.Add(canvas2d);
    canvas.Children.Add(canvas1d);

    Sunday, March 23, 2014 4:51 PM

All replies

  • I am not sure what you are trying to do here.

    Canvas canvas1d = new Canvas();
    canvas1d.Height = canvas1.ActualHeight; 
    canvas1d.Width = canvas1.ActualWidth; 
    canvas1d = canvas1;
    Canvas canvas2d = new Canvas();
    canvas2d.Height = canvas1.ActualHeight;
    canvas2d.Width = canvas1.ActualWidth;
    canvas2d = canvas2;
    Canvas canvas = new Canvas();
    canvas.Height = canvas1.ActualHeight; 
    canvas.Width = canvas1.ActualWidth;
    canvas.Children.Add(canvas2d);
    canvas.Children.Add(canvas1d);

    You create a new canvas set its height and then set it equal to one of the canvas on the page already.   Then you try and add it to another canvas so you would get the error you are getting.  removing the lines of code canvas1d=canvas1; and canvas2d = canvas2;  will prevent the error from happening

    Sunday, March 23, 2014 1:24 PM
  • actually i want to set canvas1 and canvas2 as children of canvas : 
    Canvas canvas = new Canvas();
    canvas.Height = canvas1.ActualHeight; 
    canvas.Width = canvas1.ActualWidth;
    canvas.Children.Add(canvas2);
    canvas.Children.Add(canvas1);
    but it is given the same  error:
    An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code.Element is already the child of another element
     – 

    Sunday, March 23, 2014 2:08 PM
  • canvas1, and canvas2 are on the xaml page already so you can make it a child of another control.
    Sunday, March 23, 2014 3:12 PM
  • Please try to understand ! I want to do it from backend ( on button click)
    Sunday, March 23, 2014 3:26 PM
  • I do understand that.   This code should work.  

    Canvas canvas1d = new Canvas();
    canvas1d.Height = canvas1.ActualHeight; 
    canvas1d.Width = canvas1.ActualWidth; 
    
    Canvas canvas2d = new Canvas();
    canvas2d.Height = canvas1.ActualHeight;
    canvas2d.Width = canvas1.ActualWidth;
    
    Canvas canvas = new Canvas();
    canvas.Height = canvas1.ActualHeight; 
    canvas.Width = canvas1.ActualWidth;
    canvas.Children.Add(canvas2d);
    canvas.Children.Add(canvas1d);

    Sunday, March 23, 2014 4:51 PM