[英]Updating the View when items are added or removed from a bound ObservableCollection
I am currently writing a Windows 8.1 app that uses MVVM. 我目前正在编写使用MVVM的Windows 8.1应用程序。 I've stayed away from the model simply because I have never properly been able to update the View when data bound to the View changes.
我之所以离开模型,仅仅是因为绑定到View的数据发生更改时,我从未能够正确地更新View。 No number of websites or tutorials have been able to explain how to properly use INotifyPropertyChanged and I'm just lost at this point.
没有多少网站或教程能够解释如何正确使用INotifyPropertyChanged,而我在这一点上迷失了。 I have the following class (including the method for adding an item of that type).
我有以下课程(包括添加该类型项目的方法)。
public class Organization
{
public Guid Id { get; set; }
public bool IsShared { get; set; }
public string Name { get; set; }
public ObservableCollection<Event> Events { get; set; }
public async static void Add(string Name)
{
Guid Id = Guid.NewGuid();
string FileName = Id.ToString() + ".txt";
var Folder = ApplicationData.Current.LocalFolder;
try
{
var Organizations = await Folder.CreateFolderAsync("Organizations", CreationCollisionOption.FailIfExists);
StorageFile File = await Organizations.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(File, JsonConvert.SerializeObject(new Organization { Id = Id, Name = Name, Events=new ObservableCollection<Event>() }));
}
catch
{
}
}
}
The following is my ViewModel: 以下是我的ViewModel:
public class OrganizationsViewModel : Base
{
private ObservableCollection<Organization> _List = new ObservableCollection<Organization>();
public ObservableCollection<Organization> List
{
get
{
Retrieve();
return _List;
}
set
{
}
}
public async void Retrieve()
{
var Folder = ApplicationData.Current.LocalFolder;
try
{
StorageFolder Organizations = await Folder.GetFolderAsync("Organizations");
var List = await Organizations.GetFilesAsync();
foreach (StorageFile i in List)
{
try
{
using (Stream s = await i.OpenStreamForReadAsync())
{
using (StreamReader sr = new StreamReader(s))
{
var item = JsonConvert.DeserializeObject<Organization>(await sr.ReadToEndAsync());
_List.Add(item);
}
}
}
catch
{
}
}
}
catch
{
}
}
}
The Base being: 该基地是:
public class Base : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
// property changed
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
What code do I need in my ViewModel and/or my class definition that will allow the View to properly update when items are added or removed from my ObservableCollection and more importantly why does that given code work? 我需要在ViewModel和/或类定义中使用哪些代码,以便在从ObservableCollection中添加或删除项时正确地更新View,更重要的是为什么给定的代码可以正常工作? Thanks in advance!
提前致谢!
With databinding View will automatically updated as long as it notified that properties it bound to has been changed. 使用数据绑定,只要View通知它绑定到的属性已更改,它将自动更新。 So what you need is to raise property changed event whenever binding source property value changed.
因此,您需要的是在绑定源属性值更改时引发属性更改事件。 For example :
例如 :
public class OrganizationsViewModel : Base
{
private ObservableCollection<Organization> _List = new ObservableCollection<Organization>();
public ObservableCollection<Organization> List
{
get
{
Retrieve();
return _List;
}
set
{
if(_List != value)
{
_List = value;
NotifyPropertyChanged("List");
}
}
}
...
...
}
However, ObservableCollection
should automatically notify View whenever item added to or removed from collection without you raise the event manually. 但是,无论何时将项目添加到集合或从集合中删除,
ObservableCollection
都应自动通知View,而无需手动引发事件。 So I am not 100% sure where is the problem in your code. 因此,我不确定100%是否在您的代码中出了问题。 Just try to call
NotifyPropertyChanged
on setter of every property and see if the problem solved. 只需尝试在每个属性的setter上调用
NotifyPropertyChanged
,看看问题是否解决。 At least you know how to use INotifyPropertyChanged
now :) 至少您知道现在如何使用
INotifyPropertyChanged
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.