【发布时间】:2012-02-26 10:55:14
【问题描述】:
我有类人:
public class Person : INotifyPropertyChanged
{
private String name = "";
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
NameProperty = "hi";
}
public Person(String _name)
{
NameProperty = _name;
}
public String NameProperty
{
get { return name; }
set
{
name = value;
OnPropertyChanged("NameProperty");
}
}
public override String ToString()
{
return NameProperty;
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
我可以将我的类的实例绑定到 TextBox:
myPerson = new Person("demas");
Binding myBinding = new Binding("NameProperty");
myBinding.Source = myPerson;
txtName.SetBinding(TextBox.TextProperty, myBinding);
现在我收集了我的班级并想将其绑定到 ListBox:
List<Person> myCollection = new List<Person>(); // on the MainWindow level
myCollection.Add(new Person("one"));
myCollection.Add(new Person("two"));
myCollection.Add(new Person("three"));
所以我必须在 ListBox 中看到三个值:一、二、三。我该怎么做?
【问题讨论】:
-
如果我的收藏是动态创建的,我只是不知道该怎么做。可以给个代码示例吗?
标签: c# .net wpf data-binding