A collection of StackPanel as ItemsSource of ComboBox

戏子无情 提交于 2019-12-13 23:26:43

问题


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:


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>


来源:https://stackoverflow.com/questions/9052228/a-collection-of-stackpanel-as-itemssource-of-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!