如何使 observableCollection 在项目更新时通知,而不仅仅是在添加或删除时通知?
How to make an observableCollection notify when an item is updated and not only when is added or removed?
我有一个绑定到 ObservableCollection 的 ItemsControl 对象。
这是我的 ItemsControl:
<ItemsControl x:Name="AvailableProjects" ItemsSource="{Binding ProjectsList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox x:Name="IsProjectSelected" IsChecked="{Binding IsProjectSelected}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是我的 ObservableCollection:
public ObservableCollection<ProjectInfo> ProjectsList { get; set; }
我希望当用户按下复选框时,observableCollection 的 "CollectionChanged" 事件被触发,但它不起作用。我注意到复选框项正在处理事件,但 ObservableCollection 似乎没有注意到。有人可以帮我吗?提前致谢!
ObservableCollection
目的是通知集合的更改,通知对象的修改,您必须在集合中包含的对象中实现INotifyPropertyChanged
。
最终解决方案
(感谢 Mattia Magosso 和 Vijaya Krishna Paruchuri 的帮助)。
PS:我向此自定义 ObservableCollection 添加了一个新事件 "ItemChanged",每次更新项目时都会触发该事件
using System;
namespace HRGEnvironmentTool.Custom
{
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
/// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">
public class ItemsChangeObservableCollection<T> :
ObservableCollection<T> where T : INotifyPropertyChanged
{
public delegate void ItemChangedEventHandler(object source, EventArgs args);
/// <summary>
/// Event fired when an item of the collection is updated
/// </summary>
public event ItemChangedEventHandler ItemChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
RegisterPropertyChanged(e.NewItems);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
UnRegisterPropertyChanged(e.OldItems);
}
else if (e.Action == NotifyCollectionChangedAction.Replace)
{
UnRegisterPropertyChanged(e.OldItems);
RegisterPropertyChanged(e.NewItems);
}
base.OnCollectionChanged(e);
}
protected override void ClearItems()
{
UnRegisterPropertyChanged(this);
base.ClearItems();
}
private void RegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void UnRegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnItemChange();
}
protected virtual void OnItemChange()
{
if (ItemChanged != null)
{
ItemChanged(this, EventArgs.Empty);
}
}
}
}
我有一个绑定到 ObservableCollection 的 ItemsControl 对象。
这是我的 ItemsControl:
<ItemsControl x:Name="AvailableProjects" ItemsSource="{Binding ProjectsList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox x:Name="IsProjectSelected" IsChecked="{Binding IsProjectSelected}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是我的 ObservableCollection:
public ObservableCollection<ProjectInfo> ProjectsList { get; set; }
我希望当用户按下复选框时,observableCollection 的 "CollectionChanged" 事件被触发,但它不起作用。我注意到复选框项正在处理事件,但 ObservableCollection 似乎没有注意到。有人可以帮我吗?提前致谢!
ObservableCollection
目的是通知集合的更改,通知对象的修改,您必须在集合中包含的对象中实现INotifyPropertyChanged
。
最终解决方案 (感谢 Mattia Magosso 和 Vijaya Krishna Paruchuri 的帮助)。
PS:我向此自定义 ObservableCollection 添加了一个新事件 "ItemChanged",每次更新项目时都会触发该事件
using System;
namespace HRGEnvironmentTool.Custom
{
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
/// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">
public class ItemsChangeObservableCollection<T> :
ObservableCollection<T> where T : INotifyPropertyChanged
{
public delegate void ItemChangedEventHandler(object source, EventArgs args);
/// <summary>
/// Event fired when an item of the collection is updated
/// </summary>
public event ItemChangedEventHandler ItemChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
RegisterPropertyChanged(e.NewItems);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
UnRegisterPropertyChanged(e.OldItems);
}
else if (e.Action == NotifyCollectionChangedAction.Replace)
{
UnRegisterPropertyChanged(e.OldItems);
RegisterPropertyChanged(e.NewItems);
}
base.OnCollectionChanged(e);
}
protected override void ClearItems()
{
UnRegisterPropertyChanged(this);
base.ClearItems();
}
private void RegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void UnRegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnItemChange();
}
protected virtual void OnItemChange()
{
if (ItemChanged != null)
{
ItemChanged(this, EventArgs.Empty);
}
}
}
}