0

I am working on c# silverlight5 and i am using mvvm approach to do so. I have a situation where i have to create container in xaml which could accomodate the GUI which contains combo box and labels and text boxes which are created using c# code.

I then i have to bind this container created in xaml to use it in c#.

This container could be something like stackpanel on which i can have combobox and textbox and label which are created in c#.

Conclusion:

(1)Xaml file will consist of container and binding . (2) c# code will contain the code for combo box and text box and label and binds the this on container created in xaml.

Could some one please give me a small sample doing this. would be a big help.

Sss
  • 1,519
  • 8
  • 37
  • 67

1 Answers1

1

You could use a UserControl because it's very easy to access UI elements from the code-behind. Example: we want to create a UserControl named DynamicContent:

<UserControl x:Class="DynamicContent">
    <Grid x:Name="LayoutRoot"></Grid>
</UserControl>

And code-behind:

public partial class DynamicContent : UserControl
{
    public DynamicContent(){InitializeComponent();}

    public void AddComboBox(ComboBox combobox)
    {
        LayoutRoot.Childre.Add(combobox);
    }
}

But to be honest: I'm not convinced you really need to create controls programmatically, most of the time you can solve a situation like this (when you need to create UI elements dynamically) by using DataTemplates.

Martin
  • 5,714
  • 2
  • 21
  • 41
  • thanks for the reply but question is really complex. Your logic i already know but if you really wanna know the situation please go on this link (this question is stil un answered) http://stackoverflow.com/questions/23488983/rendering-a-ui-using-ivalueconverter-using-just-c-sharp-code – Sss May 07 '14 at 08:10