DCSIMG
TreeView and ObservableCollection weird bug - Just code - Tamir Khason
Wednesday, April 18, 2007 9:51 AM Tamir Khason

TreeView and ObservableCollection weird bug

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/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

תגים:, ,

Comments

No Comments