且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF更新Datagrid绑定到ObservableCollection

更新时间:2022-06-11 03:16:58





我想,可观察的集合会自动更新collectionchangedevent上的数据网格 - 这是第一次混淆

这个事件会在一个案例中引发你(在这个集合中添加,删除..等数据)。

所以
Hi,

" i thought, that the observable collection automaticaly updates the datagrid over the collectionchangedevent" -this is a first confusion
this event will fire in oly one case when you (add, delete .. etc data whithin this collection).
So
MainGrid.ItemsSource = AddressCollection;

并不会激活这个事件! !



如果你想要所有这些东西自动工作你需要为你的数据提供MVVM模式,例如:

doesn''t fire the event!!

If you want all this stuff work automatically withot any code like below, you need, to provide MVVM pattern to your Data

for example:

public class MainViewModel
{
private ObservableCollection<address> _addressCollection;
public ObservableCollection<address> AddressCollection
{
  get{ return _addressCollection;}
  set{ _addressCollection=value; RaiseProperchyChanged("AddressCollection")}
} 
}
</address></address>




<datagrid x:name="MainGrid" itemssource="{Binding AddressCollection}" datacontext="{Binding" mode="hold" xmlns:x="#unknown" />