检查以确保我的假设是正确的。使用INotifyPropertyChanged更新ObservableCollection项目属性
我有一个ObservableCollection类。我正在调用Web服务并检索一组设备。然后我列举ObservableCollection并将每个项目设置为从Web服务检索的相应设备。我怀疑的设备具有与ObservableCollection中的项目不同的属性值,但PropertyChanged事件不会触发。
我认为这是ByDesign,并且为了让PropertyChanged事件触发,我实际上必须枚举每个属性并设置值?
例如,在下面的情况下,没有任何PropertyChanged事件在任何Device类属性上触发。
ObservableCollection<Device> Items = new ObservableCollection<Device>();
Items = LoadItems();
List<Device> devices = GetDevices();
foreach (var item in Items)
{
var currentDevice = devices.Single(d1 => d1.ID == item.ID);
item = currentDevice;
}
但是,如果我手动更新每个属性,我在业务:
ObservableCollection<Device> Items = new ObservableCollection<Device>();
Items = LoadItems();
List<Device> devices = GetDevices();
foreach (var item in Items)
{
var currentDevice = devices.Single(d1 => d1.ID == item.ID);
item.Latitude = currentDevice.Latitude;
item.Longitude= currentDevice.Longitude;
}
在上述情况下,经度和纬度火他们的活动。
由于我的课有一堆属性有没有更好的方法来做到这一点比一个一个?
嘿,这工作得很好。我在Device中的一种类型是属性类,它是类。所以我不得不调用item.Load(currentDevice)和item.Location.Load(currentDevice.Location) – 2011-02-08 17:14:22