3

I cant figureout why my ICollectionView is not refreshed. Can anyone explain what I'm doing wrong?

I've made a viewmodel like this:

class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Entity> m_entities = new ObservableCollection<Entity>();
    public ICollectionView EntitiesView { get; private set; }
    public ICollectionView HighCountView { get; private set; }

    public ViewModel()
    {
        m_entities.Add(new Entity() { Id = 1, Name = "Erik", Description = "The first" });
        m_entities.Add(new Entity() { Id = 2, Name = "Olle", Description = "The second" });
        m_entities.Add(new Entity() { Id = 3, Name = "Kim", Description = "The last" });


        EntitiesView = CollectionViewSource.GetDefaultView(m_entities);
        EntitiesView.CurrentChanged += new EventHandler(EntitiesView_CurrentChanged);

        HighCountView = new CollectionView(m_entities);
        using (HighCountView.DeferRefresh())
        {
            HighCountView.Filter = e => ((Entity)e).Count > 3;
        }

    }

    private void EntitiesView_CurrentChanged(object sender, EventArgs e)
    {
        Entity current = EntitiesView.CurrentItem as Entity;
        if(current!=null)
        {
            current.Count++;
            HighCountView.Refresh();            // Do I need this line?
            OnPropertyChanged("HighCountView"); // or this?
        }
    }

...and in my window I use it as the datacontext, like this:

public partial class MainWindow : Window
{
    private ViewModel vm = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = vm;
    }
}

...and I'm doing my bindings in the XAML-code like this:

    <ListBox Grid.Column="0" x:Name="listView1" DisplayMemberPath="Name" ItemsSource="{Binding EntitiesView}" IsSynchronizedWithCurrentItem="True" />
    <ListView Grid.Column="1" x:Name="listView2" DisplayMemberPath="Name" ItemsSource="{Binding HighCountView}" IsSynchronizedWithCurrentItem="True" />

The problem is that all three Entities is always shown in listView2 despite that I set the Filter-property. Why?

EDIT

To made the sample complete, here is the Entity-class.

class Entity : INotifyPropertyChanged
{
    private int m_id;
    public int Id
    {
        bla bla.....
    }

    private string m_name;
    public string Name
    {
        bla bla.....
    }

    private string m_description;
    public string Description
    {
        bla bla.....
    }


    private int m_count;
    public int Count
    {
        get { return m_count; }
        set
        {
            if (value != m_count)
            {
                m_count = value;
                OnPropertyChanged("Count");
            }
        }
    }

    public void Update()
    {
        Description = "Updated: " + (++Count).ToString() + " times.";
    }
Erik Z
  • 4,660
  • 6
  • 47
  • 74
  • Please add code of the Entity class. – Ekk Oct 16 '12 at 10:12
  • Now is the Entity class added. – Erik Z Oct 16 '12 at 10:27
  • Can you try this: var savedhighcount = HighCountView; HighCountView = null; OnPropertyChanged("HighCountView"); HighCountView = savedhighcount; OnPropertyChanged("HighCountView"); .....Only do this in the constructor at the bottom....not in your CurrentChanged. – Colin Smith Oct 16 '12 at 11:37
  • Doesnt make any difference... :( – Erik Z Oct 16 '12 at 12:07
  • I don't know if that's the problem, but generally speaking you don't instantiate directly a CollectionView. Instead you should create a new instance of CollectionViewSource and get the corresponding View. Let the engine decide what's the best implementation to use HighCountView = new CollectionViewSource{Source = m_entities}.View; – Dtex Oct 16 '12 at 12:59
  • Thanks, but unfortunately that doesnt fix ths problem either.... – Erik Z Oct 17 '12 at 08:59
  • Can anybody give me an WORKING example of two views over a common collection where one of the views is filtered? – Erik Z Oct 17 '12 at 09:02

1 Answers1

4

At last I found what was wrong.

If I change the line:

    HighCountView = new CollectionView(m_entities);

to this

    HighCountView = new ListCollectionView(m_entities);

then it works a expected.

I can also remove this line

        OnPropertyChanged("HighCountView"); // or this?

I hope this can help somebody!

Erik Z
  • 4,660
  • 6
  • 47
  • 74