StackPanel作为ComboBox的ItemsSource的集合 [英] A collection of StackPanel as ItemsSource of ComboBox

查看:320
本文介绍了StackPanel作为ComboBox的ItemsSource的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 StackPanel
a集合,每个包含一组动态的控件集(基于数据库值),我想将它们设置为一些 ComboBox
的ItemsSource
例如我有两个数据库值应该被生成:

 在DB中我有这些:
row 1 => Class [a] p [B],[AB] vb
row 2 => class tpy rbs = [sdfg],kssc [h] hm

每个应该像ComboBox一样生成一个ComboBox列:

 在ComboBox中我想生成这些:
ComboBoxItem 1:类[一个文本框] p [一个textBox],[一个textBox] vb
ComboBoxItem 2:类tpy rbs = [一个textBox] .kssc [一个textBox] hm
/ pre>

休闲代码正在这样做:

 类ConvertToControlsFormat()
{
Regex exp = new Regex(@\ [\w * \]);
var source = new TestEntities()。cmbSources;
foreach(来源的var item)
{
StackPanel p = new StackPanel {Orientation = Orientation.Horizo​​ntal,FlowDirection = FlowDirection.LeftToRight};
int i = 0;
foreach(exp.Split(item.Title)中的字符串txt)
{
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);
}
}

但是我不能设置 TwoWay DataBindings ,所以我创建了一个 StackPanel 的列表作为 cmbSource class(绑定到 ComboBox 的I​​temsSource)

  public partial class cmbSource 
{
#region原始属性
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.Horizo​​ntal,FlowDirection = System.Windows.FlowDirection.LeftToRight};
int i = 0;
foreach(exp.Split(ttl)中的字符串txt)
{
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标题
{
get;
设置;
}

public virtual StackPanel ControlsCollection
{
get;
设置;
}

#endregion
}

我不知道如何绑定到 ItemsSource 我的 ComboBox



Summery:我想将控件列表绑定到一个 ComboBox
任何建议!谢谢。

解决方案

编辑



首先:您不要将ComboBox绑定到UI元素集合。这不是WPF的工作方式。容器控件(如Grid,StackPanel和Canvas)可以包含子控件。 ItemsControls如ComboBox包含数据对象,并使用DataTemplates显示项目。



其次:如果数据库可以包含任何可能导致任何UI需要的数据,那么您将需要通过创建StackPanels等来生成代码中的UI,如代码示例中那样添加控件和绑定。



第三:不能绑定的原因是数据库中的数据是一个分割成部分的字符串;没有办法可以简单地回到字符串。



建议:数据库中的字符串可能(我希望)以某种格式。使用该知识,您可以在解析数据库字符串时生成新的格式字符串。例如,当数据库包含 foo [bar] 时,您可以生成 {0} [bar] 。在用户的保存操作中,您可以使用该字符串通过使用以下命令来创建数据库更新的字符串: String.Format({0} [bar],someControl.Text)



额外:请,下次使用更好的名称和示例文本;这个问题是不可读的。没有办法可以期待我们理解 2 => class tpy rbs = [sdfg],kssc [h] hm



OLD ANSWER



创建一个类Stoot,实现INotifyPropertyChanged并具有属性Name和Value。



将数据库数据加载到 ObservableCollection< Stuff> 中,并将ComboBox绑定到此集合。



将组合框的ItemTemplate设置为如下所示的数据表:

 < ComboBox ItemsSource ={Binding }> 
< ComboBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Horizo​​ntal>
< TextBlock Text ={Binding Name}/>
< TextBox Text ={Binding Value}/>
< / StackPanel>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>


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.

解决方案

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>

这篇关于StackPanel作为ComboBox的ItemsSource的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆