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

时间:2015-09-11 14:59:43

标签: wpf events observablecollection itemscontrol

我有一个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>

这是我的ObservableCollection:

public ObservableCollection<ProjectInfo> ProjectsList  { get; set; }

我希望当用户按下复选框时,&#34; CollectionChanged&#34; observableCollection的事件被触发但它无法正常工作。我注意到复选框项正在处理事件,似乎ObservableCollection没有注意到。有人可以帮我这个吗?提前致谢!

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

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

}