Reputation: 227
I have an ItemsControl object that is binded to an ObservableCollection.
Here is my 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>
And here is my ObservableCollection:
public ObservableCollection<ProjectInfo> ProjectsList { get; set; }
I would like that when the user presses the checkBox the "CollectionChanged" event of the observableCollection was fired but it's not working. I noticed that the checkbox item is handling the event and seems that the ObservableCollection doesn't notice. Someone can help me with this? Thanks in advance!
Upvotes: 2
Views: 5102
Reputation: 227
Final Solution (Thank you to Mattia Magosso and Vijaya Krishna Paruchuri for your help).
PS: I added a new event "ItemChanged" to this custom ObservableCollection that is fired everytime an item is updated
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);
}
}
}
}
Upvotes: 1
Reputation: 533
ObservableCollection
purpose are to notify the change of a collection, to notify the modification of an object you must implement INotifyPropertyChanged
in the object contained in the collection.
Upvotes: 7