【问题标题】:A collection of StackPanel as ItemsSource of ComboBoxStackPanel 的集合作为 ComboBox 的 ItemsSource
【发布时间】:2012-02-21 13:26:11
【问题描述】:

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

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

并且每一个都应该生成为一个 ComboBox 列,如下所示:

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

闲置的代码是正确的:

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);
    }
}

但我不能为此设置TwoWay DataBindings,所以我创建了一个StackPanel 列表作为cmbSource 类的字段(绑定到ComboBox 的ItemsSource)

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
}

但我不知道如何将其绑定到我的ComboBoxItemsSource

summery:我想将控件列表绑定到ComboBox 有什么建议!?谢谢。

【问题讨论】:

  • 请解释您为什么不使用 DataTemplates。每当您向 Data 类添加 UI 控件时,您都应该担心您的设计。
  • @Erno:我可以为此创建一个动态 DataTemplate 吗?
  • 您的代码太复杂(或者我太笨)无法理解您在做什么。但是,当您将 UI 元素类型属性添加到数据类时,一定有问题。
  • @Erno:你知道我必须为每个 ComboBox 项生成不同的控件集,并且在用户填充控件后,我应该将它们插入到 db 中,所以我不想松开 DataBinding。跨度>
  • 你知道期待什么样的数据吗?你能在设计时定义所有的数据模板吗?

标签: wpf data-binding wpf-controls


【解决方案1】:

编辑

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

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

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

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

补充:下次请使用更好的名称和示例文本;像这样的问题是不可读的。你不可能指望我们理解2=&gt;Class tpy rbs=[sdfg],kssc[h] hm

老答案

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

将数据库数据加载到ObservableCollection&lt;Stuff&gt; 并将 ComboBox 绑定到此集合。

将组合框的 ItemTemplate 设置为这样的数据模板:

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

【讨论】:

  • 每个 ComboBoxItem 需要不同的模板
  • 您需要的模板有什么不同?模板真的需要动态定义吗?或者您可以创建一对并使用它们吗?
  • 模板根据用户插入数据库的值生成,我不能使用一对
  • 我建议您至少添加 3 个来自数据库的数据示例和可以为您的问题生成的数据模板。很抱歉这么固执,但你还没有说服我需要生成它们。另外:您不能将组合框绑定到 UI 元素的集合;这就是网格、画布、堆栈面板等的用途。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多