C# ListBox.SetBinding-ItemSourceProperty不工作

C# ListBox.SetBinding-ItemSourceProperty不工作,c#,wpf,listbox,C#,Wpf,Listbox,我在学校项目中使用WPF。一切都很好,直到我尝试将列表绑定到列表框。我编写了一些示例代码,其中出现了问题 public MainWindow() { InitializeComponent(); //make a new source myDataObject = new TestClass(DateTime.Now); Binding myBinding = new Binding("Students");

我在学校项目中使用WPF。一切都很好,直到我尝试将列表绑定到列表框。我编写了一些示例代码,其中出现了问题

    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

    }
public主窗口()
{
初始化组件();
//寻找新的来源
myDataObject=newTestClass(DateTime.Now);
绑定myBinding=新绑定(“学生”);
myBinding.Source=myDataObject.Students;
列出学生=新建列表();
对于(int i=0;i<10;i++)
{
添加(新的TestStudent(“name”+i,“surename”+i));
}
myDataObject.Students=Students;
myList.ItemsSource=myDataObject.Student;//这很有效
myList.SetBinding(ListBox.ItemsSourceProperty,myBinding);//这不显示任何内容
}

我已经正确地实现了INotifyPropertyChanged接口。当我以这种方式将一些数据添加到任何TextBox.Text时,效果很好。但当我尝试将列表绑定到ListBox.ItemsSource时,结果是空框。为什么会发生这种情况?提前感谢您的建议。

绑定的
源代码应该是
myDataObject
,而不是
myDataObject.Students
。如果您将
myDataObject.Students
设置为源,则
ListBox
将尝试绑定到
myDataObject.Students.Students
,该对象不存在。

任何特定原因您不只是在XAMLI中执行此操作,我需要根据用户操作更改绑定,而且我在XAML中没有使用触发器的经验。我不知道,是否可以用XAML做这样的事情。好的,很好,谢谢。但还有另外两个问题。首先,列表中的更改会延迟更新(如果已更新)。第二,我有一个按钮。单击时,将执行以下行myDataObject.Students.RemoveAt(0);。这将删除列表的第一个元素。但它会删除列表底部的元素,并且通常一次删除一个或多个emelent。@Qerts,这是因为
list
在其内容被修改时不发送通知,因此绑定系统无法检测更改。改为使用
可观察的集合