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
You may also be interested with:
April 17th, 2007 · Comments (5)
5 Responses to “TreeView and ObservableCollection weird bug”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:10 am
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
}
}
January 1st, 2009 at 12:10 am
That code works like a charm!! Thanks a ton.
January 1st, 2009 at 12:10 am
Thank you, Anthony. I’ve updated the article
January 1st, 2009 at 12:10 am
This workaround provides 4 collectionchanged events. Don’t know if this will help anybody.
April 15th, 2009 at 9:58 am
This is quite a up-to-date information. I’ll share it on Digg.