【发布时间】: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
}
但我不知道如何将其绑定到我的ComboBox 的ItemsSource
summery:我想将控件列表绑定到ComboBox
有什么建议!?谢谢。
【问题讨论】:
-
请解释您为什么不使用 DataTemplates。每当您向 Data 类添加 UI 控件时,您都应该担心您的设计。
-
@Erno:我可以为此创建一个动态 DataTemplate 吗?
-
您的代码太复杂(或者我太笨)无法理解您在做什么。但是,当您将 UI 元素类型属性添加到数据类时,一定有问题。
-
@Erno:你知道我必须为每个 ComboBox 项生成不同的控件集,并且在用户填充控件后,我应该将它们插入到 db 中,所以我不想松开 DataBinding。跨度>
-
你知道期待什么样的数据吗?你能在设计时定义所有的数据模板吗?
标签: wpf data-binding wpf-controls