Wpf 如何使observableCollection在更新项时发出通知,而不仅仅是在添加或删除项时发出通知?

Wpf 如何使observableCollection在更新项时发出通知,而不仅仅是在添加或删除项时发出通知?,wpf,events,observablecollection,itemscontrol,Wpf,Events,Observablecollection,Itemscontrol,我有一个ItemsControl对象,它绑定到ObservableCollection 这是我的ItemsControl: <ItemsControl x:Name="AvailableProjects" ItemsSource="{Binding ProjectsList}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate>

我有一个ItemsControl对象,它绑定到ObservableCollection

这是我的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>

这是我的观察所得:

public ObservableCollection<ProjectInfo> ProjectsList  { get; set; }
publicobservableCollection项目列表{get;set;}

我希望当用户按下复选框时,observableCollection的“CollectionChanged”事件被触发,但它不起作用。我注意到复选框项正在处理事件,而ObservableCollection似乎没有注意到。有人能帮我吗?提前谢谢

ObservableCollection
目的是通知集合的更改,通知必须在集合中包含的对象中实现的对象的修改。

最终解决方案 (感谢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);
            }
        }
    }

}
使用系统;
命名空间HRGEnvironmentTool.Custom
{
使用系统集合;
使用System.Collections.ObjectModel;
使用System.Collections.Specialized;
使用系统组件模型;
/// 
///该类添加了在任何属性出现时刷新列表的功能
///对象在实现INotifyPropertyChanged的列表中更改。
/// 
/// 
公共类项目ChangeObservableCollection:
可观察到的集合,其中T:InotifyProperty已更改
{
公共委托void itemchangedventhadler(对象源、事件args args);
/// 
///更新集合项时激发的事件
/// 
公共事件项更改事件处理程序项更改;
CollectionChanged上的受保护覆盖无效(NotifyCollectionChangedEventArgs e)
{
if(e.Action==NotifyCollectionChangedAction.Add)
{
登记财产变更(如新项目);
}
else if(e.Action==NotifyCollectionChangedAction.Remove)
{
未注册的财产变更(如旧物品);
}
else if(e.Action==NotifyCollectionChangedAction.Replace)
{
未注册的财产变更(如旧物品);
登记财产变更(如新项目);
}
基础。变更的集合(e);
}
受保护的覆盖无效ClearItems()
{
未注册的财产变更(本);
base.ClearItems();
}
私有无效注册表属性更改(IList项)
{
foreach(项目中的INotifyPropertyChanged项目)
{
如果(项!=null)
{
item.PropertyChanged+=新的PropertyChangedEventHandler(item\u PropertyChanged);
}
}
}
私有无效未注册属性已更改(IList项)
{
foreach(项目中的INotifyPropertyChanged项目)
{
如果(项!=null)
{
item.PropertyChanged-=新的PropertyChangedEventHandler(item\u PropertyChanged);
}
}
}
私有无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
OnItemChange();
}
受保护的虚拟更改()
{
if(ItemChanged!=null)
{
ItemChanged(此为EventArgs.Empty);
}
}
}
}

我在ProjectInfo类中实现了INotifyPropertyChanged,但仍然不起作用。公共类ProjectInfo:INotifyPropertyChanged{private bool isProjectSelected;///////当前项目是否选中///公共bool isProjectSelected{get{返回this.isProjectSelected;}设置{this.isProjectSelected=value;this.OnPropertyChanged();}}你好,Mattia!首先感谢您的帮助!我的意思是,我在ProjectInfo类(ObservableCollection类型)的bool属性isProjectSelected中实现了INotifyPropertyChanged,但事件“CollectionChanged”当用户选中其中一个复选框项listHi Ricardo时,ObservableCollection的属性不会触发,可能我以前不清楚,但我的意思是,当集合的任何项的属性发生更改时,您将从
CollectionChanged
事件中收到通知。您需要做的是附加到
PropertyChanged
收藏中的每一件物品。嗨,Mattia!你说得对!非常感谢。正如a+在CodeProject中找到了一个很好的例子:可能的重复