Search code examples
wpfdata-bindingicollectionview

Why is not ICollectionView refreshed?


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.";
    }

Solution

  • 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!