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);
}}
1 comment:
has this been fixed in the framework???? Or should i use your fix if i bind to a treeview at this moment.
Post a Comment