TreeView and ObservableCollection weird bug

Recently my client points me to strange bug, if observable collection is binded to treeview and you are trying to perform Move or Replace action on the ObservableCollection, the exception “Unexpected collection change action ‘Replace’” or “Unexpected collection change action ‘Move’” will be thrown. This not happens with other ItemsControl e.g. ListBox. So, this is one of WPF confirmed bugs (which actually already fixed). How to build workaround? Simple, you do not have to know WPF in order to do it. Just replace internal method MoveItem with, e.g. InsertItem and RemoveItem and that’ll work fine. BTW, you can not do straight assignment (replace), ‘cos this will throw the same exception. So, please see proposed workaround for this problem.

    public class FixedObservableCollection<T> : ObservableCollection<T>
    {
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            //base.MoveItem(oldIndex, newIndex);
            T oItem = base[oldIndex];
            base.InsertItem(oldIndex, base[newIndex]);
            base.RemoveAt(oldIndex + 1);
 
            base.InsertItem(newIndex, oItem);
            base.RemoveAt(newIndex + 1);
 
        }    
    }

UPD 21-Apr-07: Thanks to Anthony, that points me to kind of bug with this workaround. In this case, CollectionChanged event will be fired 4 time for only one Move, action. The solution is simple, override this event and suppress firing while you do not want it. But re really problem is much more complicated. The reason for this bug is not move or replace action, but the event fired. Just to test, try fire real move event from your collection within this workaround. You’ll get into the same problem.

        protected override void MoveItem(int oldIndex, int newIndex)
        {
            //base.MoveItem(oldIndex, newIndex);
            suppress = true;
            T oItem = base[oldIndex];
            base.InsertItem(oldIndex, base[newIndex]);
            base.RemoveAt(oldIndex + 1);
            base.InsertItem(newIndex, oItem);
            base.RemoveAt(newIndex + 1);
            suppress = false;
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(
                System.Collections.Specialized.NotifyCollectionChangedAction.Move, oItem, newIndex, oldIndex));
        }
        bool suppress = false;
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (!suppress)
            {
                base.OnCollectionChanged(e);
            }
        }

 

So, what’s the solution? Real, override MoveItem event by suppressing event invocation and fire neutral event, such as Reset at the end

        protected override void MoveItem(int oldIndex, int newIndex)
        {
            suppress = true;
            base.MoveItem(oldIndex, newIndex);
            suppress = false;
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(
                System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }

        bool suppress = false;
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (!suppress)
            {
                base.OnCollectionChanged(e);
            }
        }

 

That’s all. Have a nice weekend

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • Live
  • Reddit
  • TwitThis
  • email
  • Slashdot
  • StumbleUpon

You may also be interested with:

  1. INotifyPropertyChanged auto wiring or how to get rid of redundant code

⟨ , ,  ⟩

5 Responses to “TreeView and ObservableCollection weird bug”

  1. Fedir Nepyivoda Says:

    This workaround has the side effect: the state of tree view items are lost (if they was expended, for example).

    The better solution is to subclass TreeView and TreeViewItem. Then override OnItemsChanged in both classes and do nothing.

    Like this:

       public class MyTreeView : TreeView

       {

           protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

           {

                  // do nothing

           }

           protected override DependencyObject GetContainerForItemOverride()

           {

               return new MyTreeViewItem();

           }

       }

       public class MyTreeViewItem : TreeViewItem

       {

           protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

           {

               // do nothing

           }

       }

  2. vibhor Says:

    That code works like a charm!! Thanks a ton.

  3. Tamir Khason Says:

    Thank you, Anthony. I’ve updated the article

  4. Anthony Says:

    This workaround provides 4 collectionchanged events. Don’t know if this will help anybody.

  5. How to Get Six Pack Fast Says:

    This is quite a up-to-date information. I’ll share it on Digg.

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together