Master
Master

Reputation: 2153

Notified of property change of an Item within a Collection

at the moment I currently have a Collection of users. I use the collection to populate my ItemsControl with a datatemplate of Checkboxes. I populate the first item with a Select All

Analysts.Add(new UserDTO
{
    Id         = 0,
    Name       = "Select All",
    IsSelected = true
});

I'm wondering how do I create the event such that if any of the checkboxes are ticked, an event is fired. I tried to set Analysts.CollectionChanged += Analysts_CollectionChanged; but that wouldn't really fire unless the collection is literally changed, not the item properties.

UsersDTO.cs

public int Id {get; set;}
private string _name;
public string Name
{
    get { return _name; }
    set {_name = value; OnPropertyChanged("Name");}
}

private bool _isSelected;
public bool IsSelected
{
    get { return _isSelected; }
    set { _isSelected = value; OnPropertyChanged("IsSelected"); }
}

DismissAppointmentView

<ItemsControl Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Analysts, UpdateSourceTrigger=PropertyChanged}" IsTabStop="False">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <telerik:RadUniformGrid Rows="2"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox telerik:StyleManager.Theme="Windows8" Margin="3 3 12 3" Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 1

Views: 199

Answers (1)

lokusking
lokusking

Reputation: 7456

You might want to use something like this:

 public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged
    {
        public ObservableCollectionEx() : base() { }

        public ObservableCollectionEx(List<T> list)
            : base((list != null) ? new List<T>(list.Count) : list)
        {
            CopyFrom(list);
        }

        public ObservableCollectionEx(IEnumerable<T> collection)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            CopyFrom(collection);
        }

        private void CopyFrom(IEnumerable<T> collection)
        {
            IList<T> items = Items;
            if (collection != null && items != null)
            {
                using (IEnumerator<T> enumerator = collection.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        items.Add(enumerator.Current);
                    }
                }
            }
        }

        protected override void InsertItem(int index, T item)
        {
            base.InsertItem(index, item);
            item.PropertyChanged += Item_PropertyChanged;
        }

        protected override void RemoveItem(int index)
        {
            Items[index].PropertyChanged -= Item_PropertyChanged;
            base.RemoveItem(index);
        }

        protected virtual void MoveItem(int oldIndex, int newIndex)
        {
            T removedItem = this[oldIndex];
            base.RemoveItem(oldIndex);
            base.InsertItem(newIndex, removedItem);
        }

        protected override void ClearItems()
        {
            foreach (T item in Items)
            {
                item.PropertyChanged -= Item_PropertyChanged;
            }
            base.ClearItems();
        }

        protected override void SetItem(int index, T item)
        {
            T oldItem = Items[index];
            T newItem = item;
            oldItem.PropertyChanged -= Item_PropertyChanged;
            newItem.PropertyChanged += Item_PropertyChanged;
            base.SetItem(index, item);
        }

        private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var handler = ItemPropertyChanged;
            if (handler != null) { handler(sender, e); }
        }

        public event PropertyChangedEventHandler ItemPropertyChanged;
    }

Now you can subscribe to the PropertyChanged-Event of the Elements in the Collection

Upvotes: 1

Related Questions