Dynamically add User Controls to a Silverlight 4 page

Posted by PilotBob on Stack Overflow See other posts from Stack Overflow or by PilotBob
Published on 2010-04-14T21:48:50Z Indexed on 2010/04/14 21:53 UTC
Read the original article Hit count: 377

I am building an iGoogle like "dashboard" for our application with silverlight 4.

Each users dashboard (which snapins and their positions) will be stored in the database. Each snap in is a user control, for example... AwesomeSnapin.xaml.

On the dashboard page in silverlight I am retrieving the users dashboard which is a collection of snapin objects which include the information on the snapin. I can store the name of the page or the class or whatever is needed.

I have the following code which loops through the collection of snapins to add them to the dashboard page. In testing I have just hard coded a single snapin item. Here is the prototype code:

        foreach (var UserSnapin in op.Entities)
        {
            UserControl uc = new AmsiSL.eFinancials.BudgetCheck();

            Canvas.SetLeft(uc, UserSnapin.PositionLeft);
            Canvas.SetTop(uc, UserSnapin.PositionTop);

            Layout.Children.Add(uc);

            MessageBox.Show(String.Format("Added {0}",UserSnapin.Snapin.Name));
        }  

The above works fine... but of course adds the BudgetCheck snapin for every item that is defined for the users dashboard. Of course the messagebox is for debugging purposes only.

How would I change line 3 of that to load the user control class (using classname or xaml path whichever is better) based on the data in the collection.

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-4.0