Reputation: 1005
I use WPF in school project. Everything was just fine, until I tried to bind list to ListBox. I wrote some example code, where the issue appears.
public MainWindow()
{
InitializeComponent();
//make a new source
myDataObject = new TestClass(DateTime.Now);
Binding myBinding = new Binding("Students");
myBinding.Source = myDataObject.Students;
List<TestStudent> students = new List<TestStudent>();
for (int i = 0; i < 10; i++)
{
students.Add(new TestStudent("name" + i, "surename" + i));
}
myDataObject.Students = students;
myList.ItemsSource = myDataObject.Student; //this works
myList.SetBinding(ListBox.ItemsSourceProperty, myBinding); //this doesn't show anything
}
I have properly implemented INotifyPropertyChanged interface. When I add some data this way to any TextBox.Text, it works fine. But when I try to bind list to ListBox.ItemsSource, result is empty box. Why does this happen? Thanks in advance for any advices.
Upvotes: 0
Views: 209
Reputation: 292435
The Source
of the binding should be myDataObject
, not myDataObject.Students
. If you set myDataObject.Students
as the source, the ListBox
will try to bind to myDataObject.Students.Students
, which doesn't exist.
Upvotes: 2