1
votes

I have
a collection of StackPanel which each one includes a dynamic set of controls (based on database values), I want to set them as ItemsSource of some ComboBox for example i have two database values which should be generated:

In DB i have these:
row 1=>Class [a] p [B] , [AB]vb
row 2=>Class tpy rbs=[sdfg],kssc[h] hm

and each one should generate as a ComboBox column like the fallowing:

In ComboBox I wanna generate these :
 ComboBoxItem 1 :Class [a textBox] p [a textBox] , [a textBox]vb
 ComboBoxItem 2 :Class tpy rbs=[a textBox].kssc[a textBox] hm

the fallowing code is doing this right:

Class ConvertToControlsFormat()
{
    Regex exp = new Regex(@"\[\w*\]");
    var source = new TestEntities().cmbSources;
    foreach (var item in source)
    {
        StackPanel p = new StackPanel { Orientation = Orientation.Horizontal, FlowDirection = FlowDirection.LeftToRight };
        int i = 0;
        foreach (string txt in exp.Split(item.Title))
        {
            p.Children.Add(new TextBlock { Text = txt });
            if (i < exp.Matches(item.Title).Count)
                p.Children.Add(new TextBox { Text = exp.Matches(item.Title)[i].Value, Width = 30 });
        }
        cmb.Items.Add(p);
    }
}

But I cant set TwoWay DataBindings for that, so I created a list of StackPanel as a field of cmbSource class (which is bound to ItemsSource of the ComboBox)

public partial class cmbSource
{
    #region Primitive Properties
    int iD;

    public virtual int ID
    {
        get
        {
            if (Title != null)
                ControlsCollection = SetControlsCollection(Title);
            return iD;
        }
        set
        {
            iD = value;

        }
    }

    private StackPanel SetControlsCollection(string ttl)
    {
        Regex exp = new Regex(@"\[\w*\]");
        StackPanel p = new StackPanel { Orientation = Orientation.Horizontal, FlowDirection = System.Windows.FlowDirection.LeftToRight };
        int i = 0;
        foreach (string txt in exp.Split(ttl))
        {
            p.Children.Add(new TextBlock { Text = txt });
            if (i < exp.Matches(ttl).Count)
                p.Children.Add(new TextBox { Text = exp.Matches(ttl)[i].Value, Width = 30 });
        }
        return p;
    }

    public virtual string Title
    {
        get;
        set;
    }

    public virtual StackPanel ControlsCollection
    {
        get;
        set;
    }

    #endregion
}

but I have no idea of how bind it to ItemsSource of my ComboBox

Summery:I want to bind a list of controls to a ComboBox any suggestions!? thank you.

1
Please explain why you are not using DataTemplates. Anytime you are adding UI controls to your Data classes you should be worried about your design.Emond Erno
@Erno:can I create a dynamic DataTemplate for that?Mohsen
Your code is too complex (or I am too dumb) to understand what you are doing. But when you are adding UI element types properties to your data classes something must be wrong.Emond Erno
@Erno:You know i have to generate different set of controls for/as each ComboBox Item and after user fills controls, i should insert them to db , so i dont want to loose DataBinding.Mohsen
Do you know what kind of data to expect; would you be able to define all the datatemplates at design time?Emond Erno

1 Answers

4
votes

EDIT

First: you do not bind a ComboBox to a collection of UI Elements. That is not the way WPF works. Container controls such as the Grid, StackPanel and Canvas can contain child controls. ItemsControls such as the ComboBox contain data objects and use DataTemplates to display the items.

Secondly: if the database can contain ANY data that could cause ANY UI to be needed you will need to generate the UI in code by creating StackPanels etc. adding controls and bindings as you do in your code examples.

Thirdly: the reason you can't bind is that the data from the database is a string that you split into parts; there is no way you can simply go back to the string.

Suggestion: the string in the database is probably (I hope) in some sort of format. Using that knowledge you could generate a new format string when you are parsing the database string. E.g., when the database contains foo [bar] you could generate {0} [bar]. On a save action from the user you could use that string to create the updated string for the database by using: String.Format("{0} [bar]", someControl.Text)

Extra: Please, next time, use better names and example texts; the question is unreadable like this. There is no way you can expect us to understand 2=>Class tpy rbs=[sdfg],kssc[h] hm

OLD ANSWER

Make a class Stuff, implementing INotifyPropertyChanged and having the properties Name and Value.

Load the database data into an ObservableCollection<Stuff> and bind the ComboBox to this collection.

Set the ItemTemplate of the combo box to a datatemplate like this:

<ComboBox ItemsSource="{Binding}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <TextBox Text="{Binding Value}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>