tags:

views:

24

answers:

1

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 sanity 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

+1  A: 

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.

karmicpuppet
I'm sure you're right on this, but unfortunately I think this is a case of my example not being constructed like my real case. I'm pretty sure I checked the condition you're referring to in my real case and it didn't apply. I just got ahead of myself when I found this example also appeared not to work. I'll go back to the drawing board and see if I can put together a better example (and reconfirm this isn't what's happening in my real case). Thanks!
Kevin Donn