ObservableCollection dependency property does not update when item in collection is deleted

后端 未结 3 1030
眼角桃花
眼角桃花 2020-12-09 02:58

I have a attached property of type ObservableCollection on a control. If I add or remove items from the collection, the ui does not update. However if I replace the collecti

3条回答
  •  心在旅途
    2020-12-09 03:27

    This is just to add to the answer by Thomas. In my code I do interact with the DependencyObject's properties by creating a handler object localy like below:

    private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var action = new NotifyCollectionChangedEventHandler(
                (o, args) =>
                    {
                        var calendar = d as RadCalendar;
    
                        if (calendar!= null)
                        {
                            // play with calendar's properties/methods
                        }
                    });
    
        if (e.OldValue != null)
        {
           var coll = (INotifyCollectionChanged)e.OldValue;
           // Unsubscribe from CollectionChanged on the old collection
           coll.CollectionChanged -= action;
        }
    
        if (e.NewValue != null)
        {
           var coll = (ObservableCollection)e.NewValue;
           // Subscribe to CollectionChanged on the new collection
           coll.CollectionChanged += action;
        }
    }
    

    Hope this is helpful to someone.

提交回复
热议问题