本文介绍了来自Code Behind的DataTemplate中的DataBind Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题可能已被问过好几次了。但是我登陆的所有帖子都没有解决我的问题。我有一个DataModel,它在一个可观察的集合中有一个可观察的集合。可视树应该看起来像ItemsControl-> VirtualizingStack-> Combobox。以下是简单示例,理论上项目应出现在下拉列表中,但事实并非如此。我哪里错了。
This question has probably been asked several times. However all the posts I land on does not solve my problem. I have a DataModel that has an observable collection inside an observable collection. The visual tree should look something like ItemsControl->VirtualizingStack->Combobox. The following is simple example and "theoretically" items should appear in dropdown but it doesn't. Where am I going wrong.
Public MainWindow()
{
InitializeComponent();
ObservableCollection<Tester> Tester1 = new ObservableCollection<Tester>();
Tester1.Add(new Tester("Sunny", "Jenkins"));
Tester1.Add(new Tester("Pieter", "Pan"));
ObservableCollection<Tester> Tester2 = new ObservableCollection<Tester>();
Tester2.Add(new Tester("Jack", "Sanders"));
Tester2.Add(new Tester("Bill", "Trump"));
ObservableCollection<TheT> myDataV = new ObservableCollection<TheT>();
myDataV.Add(new TheT(Tester1));
myDataV.Add(new TheT(Tester2));
IControl.ItemsSource = myDataV;
IControl.ItemTemplate = TestingDT;
}
<ItemsControl x:Name="IControl" Margin="53,375,81,63">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
//the Datatemplate
private DataTemplate TestingDT
{
get
{
DataTemplate DFT = new DataTemplate();
DFT.DataType = typeof(TheT);
FrameworkElementFactory Item = new FrameworkElementFactory(typeof(ComboBox));
Binding B = new Binding("TesterObject")
{
Source = this
};
Item.SetBinding(ComboBox.ItemsSourceProperty, B);
//Item.SetValue(ComboBox.DisplayMemberPathProperty, "Name");
DFT.VisualTree = Item;
return DFT;
}
}
//And the DATA Model
class Tester
{
public Tester(string name, string surname)
{
Name = name;
Surname = surname;
}
public string Name { get; set; }
public string Surname { get; set; }
public override string ToString()
{
return Name + " " + Surname;
}
}
class TheT
{
ObservableCollection<Tester> TesterObject;
public TheT(ObservableCollection<Tester> testerObject)
{
TesterObject = testerObject;
}
public string myDisplayName { get { return "test"; } }
public void Add(Collection<Tester> col)
{
TesterObject.Clear();
foreach (Tester t in col)
{ TesterObject.Add(t); }
}
}
推荐答案
这篇关于来自Code Behind的DataTemplate中的DataBind Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!