Wpf 无法将可观察集合绑定到datagrid

Wpf 无法将可观察集合绑定到datagrid,wpf,data-binding,wpfdatagrid,Wpf,Data Binding,Wpfdatagrid,我是WPF的新手,请原谅我的无知。我在网上读到,为了绑定工作,可观察的集合应该是公共财产。以下是我到目前为止所做的工作: 我有一个类,在这个类中我声明了可观测的,如下所示: public ObservableCollection<InventoryDto> invCollection { get; set; } public InventoryDto() { invCollection = new ObservableCollection&l

我是WPF的新手,请原谅我的无知。我在网上读到,为了绑定工作,可观察的集合应该是公共财产。以下是我到目前为止所做的工作:

我有一个类,在这个类中我声明了可观测的,如下所示:

public ObservableCollection<InventoryDto> invCollection { get; set; }
 public InventoryDto()
        {
            invCollection = new ObservableCollection<InventoryDto>();
        }


        public ObservableCollection<InventoryDto> observableList
        {
            get
            {
                return this.invCollection;
            }
            set
            {
                invCollection = value;
                OnPropertyChanged("observableList");
            }
        }
ItemsSource="{Binding observableList,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
在视图的代码隐藏(即view.xamls.cs)中,我填充了可观察对象:

  foreach (var item in inventoryList)
            {

                invDto.invCollection.Add(new InventoryDto { location = item.location.Name, onHand = item.OnHand.ToString(), committed = item.committed.ToString() });


            }
但是,除非添加以下代码,否则我在网格中看不到任何数据:

 InventoryDataGrid.ItemsSource = invDto.observableList;
首先,我发现在代码隐藏中添加此代码很奇怪,而我已经在xaml中指定了ItemsSource。其次,该代码填充网格。但是当我从db中删除一行时,该行不会在网格中删除,尽管我已经指定了BindingMode=TwoWay


请让我知道我错过了什么

{Binding observateList}
这样没有源、相对源或元素名的绑定使用当前的
DataContext
作为源对象。您应该将主窗口的DataContext设置为InventoryDto类的实例。除此之外,设置
Mode=TwoWay
UpdateSourceTrigger=PropertyChanged
是毫无意义的。在这里没有效果。您也不应将公共属性用作另一公共属性的“支持字段”。也就是说,
invCollection
应该是一个私有字段。我不知道您为什么将其标记为重复的bcs,我真的不理解另一个线程。不管怎样,当你说我应该将主窗口的datacontext设置为InventoryTo的实例时,你的意思是这样的:datacontext=“{Binding invDto}”?如前所述,“将主窗口的datacontext设置为InventoryDto类的实例”,例如
datacontext=new InventoryDto()在主窗口的构造函数中。继续之前,请阅读。