C# 如何获取ObservableCollection对象更改通知
在ObservableCollection中添加新项或更新现有项时,是否有方法获取通知或事件。说C# 如何获取ObservableCollection对象更改通知,c#,wpf,C#,Wpf,在ObservableCollection中添加新项或更新现有项时,是否有方法获取通知或事件。说 class Notify:INotifyPropertyChanged { private string userID { get; set; } public string UserID { get { return userID; } set { if (userID != value)
class Notify:INotifyPropertyChanged
{
private string userID { get; set; }
public string UserID
{
get { return userID; }
set
{
if (userID != value)
{
userID = value;
OnPropertyChanged("UserID");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
class MainClass
{
ObservableCollection<Notify> notifyme = new ObservableCollection<Notify>();
changed()
{
//logic where there is an update
}
}
类通知:INotifyPropertyChanged
{
私有字符串用户标识{get;set;}
公共字符串用户ID
{
获取{return userID;}
设置
{
if(userID!=值)
{
userID=值;
OnPropertyChanged(“用户ID”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
类主类
{
ObservableCollection notifyme=新的ObservableCollection();
改为
{
//存在更新的逻辑
}
}
何时调用changed()只有一种方法可以做到这一点:在将每个项目添加到ObservableCollection时(或之前),在每个项目上连接一个事件处理程序
notifyme.Add(new Notify{ PropertyChanged += (o, e) => { do whatever }});
这是因为ObservableCollection只是一个容器,其中的每个项目都必须单独连接起来。当然,您可以编写自己的扩展类(或扩展方法)来帮助实现自动化 真正做到这一点的方法只有一种:在将每个项目添加到ObservableCollection时(或之前),在每个项目上连接一个事件处理程序
notifyme.Add(new Notify{ PropertyChanged += (o, e) => { do whatever }});
这是因为ObservableCollection只是一个容器,其中的每个项目都必须单独连接起来。当然,您可以编写自己的扩展类(或扩展方法)来帮助实现自动化 我认为INotifyPropertyChanged通知propertychanged事件,但这里我认为您的集合已更改。因此,您必须引发CollectionChanged事件 我建议你调查一下
希望这对你有帮助 我认为INotifyPropertyChanged通知propertychanged事件,但这里我认为您的集合已更改。因此,您必须引发CollectionChanged事件 我建议你调查一下
希望这对你有帮助 你可以用这样的东西
public class NotifyCollection
{
private readonly ObservableCollection<Notify> collection;
public NotifyCollection()
{
collection = new ObservableCollection<Notify>();
collection.CollectionChanged += collection_CollectionChanged;
}
private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if ((e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace) && e.OldItems != null)
{
foreach (var oldItem in e.OldItems)
{
var item = (Notify)oldItem;
try
{
item.PropertyChanged -= notify_changes;
}
catch { }
}
}
if((e.Action==NotifyCollectionChangedAction.Add || e.Action==NotifyCollectionChangedAction.Replace) && e.NewItems!=null)
{
foreach(var newItem in e.NewItems)
{
var item = (Notify)newItem;
item.PropertyChanged += notify_changes;
}
}
notify_changes(null, null);
}
private void notify_changes(object sender, PropertyChangedEventArgs e)
{
//notify code here
}
public ObservableCollection<Notify> Collection
{
get
{
return collection;
}
}
}
公共类NotifyCollection
{
私有只读可观察集合集合;
公共收藏()
{
集合=新的ObservableCollection();
collection.CollectionChanged+=collection\u CollectionChanged;
}
私有无效集合\u集合已更改(对象发送方,NotifyCollectionChangedEventArgs e)
{
if((e.Action==NotifyCollectionChangedAction.Remove | | e.Action==NotifyCollectionChangedAction.Replace)和&e.OldItems!=null)
{
foreach(e.OldItems中的var oldItem)
{
变量项=(通知)旧项;
尝试
{
item.PropertyChanged-=通知变更;
}
捕获{}
}
}
if((e.Action==NotifyCollectionChangedAction.Add | | e.Action==NotifyCollectionChangedAction.Replace)&&e.NewItems!=null)
{
foreach(e.NewItems中的var newItem)
{
var item=(Notify)newItem;
item.PropertyChanged+=通知变更;
}
}
通知变更(空,空);
}
私有void notify_更改(对象发送方,PropertyChangedEventArgs e)
{
//在这里通知代码
}
公开收集
{
得到
{
回收;
}
}
}
您可以使用类似的方法
public class NotifyCollection
{
private readonly ObservableCollection<Notify> collection;
public NotifyCollection()
{
collection = new ObservableCollection<Notify>();
collection.CollectionChanged += collection_CollectionChanged;
}
private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if ((e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace) && e.OldItems != null)
{
foreach (var oldItem in e.OldItems)
{
var item = (Notify)oldItem;
try
{
item.PropertyChanged -= notify_changes;
}
catch { }
}
}
if((e.Action==NotifyCollectionChangedAction.Add || e.Action==NotifyCollectionChangedAction.Replace) && e.NewItems!=null)
{
foreach(var newItem in e.NewItems)
{
var item = (Notify)newItem;
item.PropertyChanged += notify_changes;
}
}
notify_changes(null, null);
}
private void notify_changes(object sender, PropertyChangedEventArgs e)
{
//notify code here
}
public ObservableCollection<Notify> Collection
{
get
{
return collection;
}
}
}
公共类NotifyCollection
{
私有只读可观察集合集合;
公共收藏()
{
集合=新的ObservableCollection();
collection.CollectionChanged+=collection\u CollectionChanged;
}
私有无效集合\u集合已更改(对象发送方,NotifyCollectionChangedEventArgs e)
{
if((e.Action==NotifyCollectionChangedAction.Remove | | e.Action==NotifyCollectionChangedAction.Replace)和&e.OldItems!=null)
{
foreach(e.OldItems中的var oldItem)
{
变量项=(通知)旧项;
尝试
{
item.PropertyChanged-=通知变更;
}
捕获{}
}
}
if((e.Action==NotifyCollectionChangedAction.Add | | e.Action==NotifyCollectionChangedAction.Replace)&&e.NewItems!=null)
{
foreach(e.NewItems中的var newItem)
{
var item=(Notify)newItem;
item.PropertyChanged+=通知变更;
}
}
通知变更(空,空);
}
私有void notify_更改(对象发送方,PropertyChangedEventArgs e)
{
//在这里通知代码
}
公开收集
{
得到
{
回收;
}
}
}