locked
Error on creating dynamically textblock on stack panel from foreach loop

    Question

  • Hey guys, 

    I am never had this issue before, but i am generating labels on my stack panel dynamically from foreach loop. but the issue is weird which tells me like this .

    An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code
    WinRT information: Element is already the child of another element.
    Additional information: No installed components were detected.
    Element is already the child of another element.
    If there is a handler for this exception, the program may be safely continued.

    TextBlock Label;
            private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                var param = e.NavigationParameter as List<SignGame.DataModel.Result_Signs>;
     
                List<SignGame.DataModel.Result_Signs> Results = param;
                Label = new TextBlock() { FontSize = 25, TextAlignment=TextAlignment.Center};
                foreach (var item in param)
                {
                    Label.Name = item.Question.ToString();
                    Label.Text = item.Question.ToString();
                    Questions.Children.Add(Label);//Questions is my stack panel
                }
            }

    Thanks in advance :) for helping and reviewing it

    Tuesday, November 25, 2014 10:45 AM

Answers

  • Hi,

    You are getting the error because you are trying to add the same label each time.

     just move the following line inside foreach

     Label = newTextBlock() { FontSize = 25, TextAlignment=TextAlignment.Center};

    This will solve the issue.


             

    • Proposed as answer by sharmi04 Tuesday, November 25, 2014 12:17 PM
    • Marked as answer by usmankai Thursday, November 27, 2014 4:47 PM
    Tuesday, November 25, 2014 12:17 PM