开发者

CollectionView with Filter not updating after ObservableCollection<T>.Move

Using .NET 3.5, I'm finding that a CollectionView with a filter doesn't update when the underlying ObservableCollection(T) has items moved using ObservableCollection(T).Move. As a s开发者_如何学Goanity check, I started with a simple project, one of Bea Stollnitz' from this page: http://bea.stollnitz.com/blog/?p=31. The actual project can be downloaded here: bea.stollnitz.com/files/28/FilterSample.zip. If you go into her Window1.xaml file and add a button like this:

<StackPanel>

         <Button Content="Blah" Click="Button_Click"/>

and then add the click handler like this:

      private void Button_Click(object sender, RoutedEventArgs e) {
GreekGods src1 = this.Resources["src1"] as GreekGods;
         src1.Move(0, 1);
      }

You'll find that pressing the button does not result in any change in the window. However, if you comment out the line of code in her constructor:

     collectionView.Filter = new Predicate<object>(FilterOutA);

and then press the button, you'll see the two top elements swap position.

So what am I missing? Is this a bug, or does CollectionView just lose this functionality when you apply a filter? I can understand if you've got the CollectionView sorted, then changing the underlying order wouldn't change the CollectionView's order, but that's not true here. Shouldn't just having a Filter on the CollectionView continue to show the underlying ObservableCollection's order and update as it changes?

Thanks, Kevin


No, it's not a bug and is working perfectly. See, in that example, here's the order of items in the source collection:

[0] Venus
[1] Apollo
[2] Mars

Now, if you apply "FilterOutA", "Apollo" will be removed and thus the listbox will just show:

[0] Venus
[1] Apollo <-Filtered out
[2] Mars

Given this, calling src1.Move(0,1) will essentially move "Venus" to index 1. Thus, in the filtered collection, it will appear like nothing changed.

[0] Apollo <-Filtered out
[1] Venus
[2] Mars

The thing to note here is that the Move() method works on the original collection and not the filtered one.

Try changing the move operation to src1.Move(0,2), and you'll see the UI update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜