为什么我的列表ObservableCollection从不刷新? [英] Why my list ObservableCollection never is refresh?
问题描述
为什么从不刷新我的列表?我正在使用可观察的列表
private ObservableCollection<Actor> actors = new ObservableCollection<Actor>();
public ObservableCollection<Actor> Actors
{
get
{
return actors;
}
set
{
actors = value;
RaisePropertyChanged(() => Actors);
}
}
for (int i=0; i<actors.Count;i++) {
actors[i].IsHidden = false;
}
仅当我清理并重新插入所有数据时才会刷新
ObservableCollection
仅在项目更改时(即您在集合中添加或删除项目时所说的)通知更改. >
如果您还希望在类的某些属性更改时收到通知,则需要使该类实现INotifYPropertyChange
接口.
在您的特定情况下,您将需要更新Actor
类.
public class Actor : INotifyPropertyChanged
{
...
private bool isHidden;
public bool IsHidden
{
get {return isHidden}
set
{
isHidden = value;
RaisePropertyChanged(() => IsHidden);
}
}
...
}
您必须像在ViewModel中一样对所有要监听更改的属性执行此操作.
可能您已经完成了此操作,但是我建议您添加一个BaseViewModel
,它基本上是具有INotifyPropertyChanged
实现的类,并创建ViewModels以及该类和您希望此行为扩展的其他任何类.
希望这会有所帮助.-
Why never refresh my list? i am using list observable
private ObservableCollection<Actor> actors = new ObservableCollection<Actor>();
public ObservableCollection<Actor> Actors
{
get
{
return actors;
}
set
{
actors = value;
RaisePropertyChanged(() => Actors);
}
}
for (int i=0; i<actors.Count;i++) {
actors[i].IsHidden = false;
}
only its refresh when i clean and reinsert my all datas
An ObservableCollection
will only Notify of a change when its items change, that is, as you stated when items are added or removed from the collection.
If you want to also be notified when certain properties of your class change you will need to make that class implement INotifYPropertyChange
interface.
In your specific case, you will need to update Actor
class.
public class Actor : INotifyPropertyChanged
{
...
private bool isHidden;
public bool IsHidden
{
get {return isHidden}
set
{
isHidden = value;
RaisePropertyChanged(() => IsHidden);
}
}
...
}
You will have to do this for all the properties you want to listen for changes as you did in your ViewModel.
Probably you already have done this, but I suggest you add a BaseViewModel
which is basically a class with the INotifyPropertyChanged
implementation and make your ViewModels and also this class and any other class you want this behavior to extend it.
Hope this helps.-
这篇关于为什么我的列表ObservableCollection从不刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!