Sss
Sss

Reputation: 1529

How to create XAML container for combo box which is created?

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.

Upvotes: 0

Views: 180

Answers (1)

Martin
Martin

Reputation: 6156

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.

Upvotes: 1

Related Questions