How to Add Objects to Stack Panel in Code-Behind

Here's how to dynamically add objects to a StackPanel in code-behind:

On our XAML page add a StackPanel tag and add a x:Name attrribute:

    <stackpanel horizontalalignment="Left" x:name="Stack1">
    
    </stackpanel>

In the code-behind create the new element and add it to the Children collection:

    public MainPage()
    {          
        TextBox TextObj;

        InitializeComponent();

        TextObj = new TextBox();
        TextObj.Width = 100;

        Stack1.Children.Add(TextObj);
    }


Site Map | Printable View | © 2008 - 2012 Sandy Pond Consulting | Powered by mojoPortal | HTML 5 | CSS | Design by styleshout