Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ListView doesn't Filter after adding new Item to the list.

$
0
0

I'm creating an application that consists of multiple filters and a ListView which displays data from an Entity Framework database. 

Within my application, I get a strange behavior that occurs within my listview when I add a new item to a list.

When I first load my application, I am able to filter through the list of items that is presented within the `listview` with no problems. However, when Iadd an item to this list, the filter doesn't work at all.

Within my view, I bind my listview the an ObservableCollection which is within a class called CourseListViewModel, like so;

<ListView ... ItemsSource="{Binding CourseList}">

    private ObservableCollection<CourseViewModel> courseList = null;
    public ObservableCollection<CourseViewModel> CourseList
    {
        get { return courseList; }
        set
        {
             courseList = value;
             OnPropertyChanged("CourseList");
        }
    }

And, within my constructor, I have done something like so;

     public CourseListViewModel()
        {
            CourseList = new ObservableCollection<CourseViewModel>(GetCourse());
            CollectionViewSource.GetDefaultView(CourseList).Filter = new Predicate<object>(MainFilter);
        }

Due to the use of the `singleton pattern`, I add an item within the `CourseViewModel` like so;

public CourseListViewModel Course
        {
            get { return CourseListViewModel.Instance(); }
        }

    private void Update()
        {
            if (this.Mode == Mode.Add)
            {
                courserep.AddCourse(this); //courserep is referring to my repository which adds a course to the EF database
                MessageBox.Show("A course has been added successfully.");

                this.Course.CourseList = this.Course.GetCourse();
            }

As you can see, I use this.Course.CourseList = this.Course.GetCourse(); to update my listview when a new entry has been added.

However, I think that is where my issue lies. Therefore, I was wondering if there was an alternative way to update the listview when a new entry has been added. I have put together a sample of the whole problem whichcan be found here. Any help or guidance is very much appreciated. Thanks in advance. 


Viewing all articles
Browse latest Browse all 18858

Latest Images

Trending Articles



Latest Images