Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am working on a Windows Phone 7 application and I have a method
C#
public void PaintButtons()
        {
            IList p1numlist = GenerateRandom();
            IList p2numlist = GenerateRandom(); 
            p1b1.Content = images.ElementAt((int)p1numlist[0]);
            p1b2.Content = images.ElementAt((int)p1numlist[1]); 
            p1b3.Content = images.ElementAt((int)p1numlist[2]);
            p2b1.Content = images.ElementAt((int)p2numlist[0]); //This is the line where the exception is generated. Exception: System.InvalidOperationException, Message: Element is already the child of another element
            p2b2.Content = images.ElementAt((int)p2numlist[1]);
            p2b3.Content = images.ElementAt((int)p2numlist[2]);
            //p1b1,p1b2,p1b3,p2b1,p2b2,p2b3 are buttons
            //GenerateRandom() method returns a List of unique random numbers of size 3
            
        }

// I am adding images to the <code>images</code> collection using the following code
 
        IList<Image> images = new List<Image>();
        
        public Page1()
        {
            InitializeComponent();
            LoadImages();
        }
        public void LoadImages()
        {
            Uri[] uri = new Uri[3];
            uri[0] = new Uri("/BtnPics/Blue.png", UriKind.Relative);
            uri[1] = new Uri("/BtnPics/Red.png", UriKind.Relative);
            uri[2] = new Uri("/BtnPics/Yellow.png", UriKind.Relative);
            foreach (Uri u in uri)
            {
                BitmapImage bmp = new BitmapImage(u);
                images.Add(new Image() { Source = bmp });
                
            }
        }


I am stuck at this point, please help. The XAML content is as follows:
HTML
<grid height="800" name="LayoutRoot" width="478">
        <grid.columndefinitions>
            <columndefinition width="65*" />
            <columndefinition width="413*" />
        </grid.columndefinitions>
        <Button Height="122" HorizontalAlignment="Left" Margin="-12,682,0,0" Name="p1b1" VerticalAlignment="Top" Width="170" Grid.ColumnSpan="2" TabIndex="3" />
        <Button Height="122" HorizontalAlignment="Left" Margin="85,682,0,0" Name="p1b2" VerticalAlignment="Top" Width="170" Grid.Column="1" TabIndex="2" />
        <Button Height="122" HorizontalAlignment="Left" Margin="248,682,0,0" Name="p1b3" VerticalAlignment="Top" Width="170" Grid.Column="1" TabIndex="1" />
        <textblock height="68" horizontalalignment="Left" margin="60,618,0,0" name="player1textBlock" text="TextBlock" verticalalignment="Top" width="223" grid.column="1" />
        <textblock height="68" horizontalalignment="Left" margin="61,116,0,0" name="player2textBlock" text="TextBlock" verticalalignment="Top" width="223" grid.column="1" />
        <Image Height="220" HorizontalAlignment="Left" Margin="45,260,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="250" Grid.Column="1" />
        <Button Grid.ColumnSpan="2" Height="122" HorizontalAlignment="Left" Margin="-12,-4,0,0" Name="p2b3" TabIndex="3" VerticalAlignment="Top" Width="170" />
        <Button Grid.Column="1" Height="122" HorizontalAlignment="Right" Margin="0,-4,158,0" Name="p2b2" TabIndex="2" VerticalAlignment="Top" Width="170" />
        <Button Grid.Column="1" Height="122" HorizontalAlignment="Left" Margin="248,-4,0,0" Name="p2b1" TabIndex="1" VerticalAlignment="Top" Width="170" />
    </grid>


Can somebody help?
Posted
Updated 24-Oct-11 9:36am
v2
Comments
Sergey Alexandrovich Kryukov 24-Oct-11 15:15pm    
Not clear. Nothing in the C# code above seems related to the XAML except Button name. All buttons are virtually the same. So, it makes me doubt you really have exception in this line. Could you catch exception and dump Exception.Stack, mark the code line numbers in code sample according the exception stack?
--SA

1 solution

As I realized, images is a collection of FrameworkElements (Images).


A FrameworkElement can has only one parent in the logical-tree. (The FrameworkElement class has a Parent property that contains the one and only parent of the element.)


When you set the Content property of the Button to an Image, the Parent property of the Image is set to the Button and, when you try to set the same Image to another Button, you get the exception because the Image already has a parent.


You have to create a different Image for each Button.

 
Share this answer
 
v3
Comments
VigneshPT 24-Oct-11 15:17pm    
Oh. Why would that generate an exception. I am only reading from the collection. My question maybe silly, but I am not sure about why this is happening, and what I am supposed to do. Can you be more elaborate?
VigneshPT 24-Oct-11 16:08pm    
Thanks a lot Zang. There is another question regarding this, is there a way to copy the image list to a another list, so that i can use that list without having the earlier conflict.
Shmuel Zang 24-Oct-11 16:57pm    
If you copy the values of the list to another list, you copy references (so, you actually have the same images). You can create a list with new images and, initiate them with the same bitmaps.
VigneshPT 25-Oct-11 10:32am    
Ok. Thanks for the help!!
VigneshPT 25-Oct-11 10:33am    
Ok. Thanks for the help Zang!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900