Results 1 to 3 of 3

Thread: ListBox ItemsSource databinding issue (MVVM Pattern)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    ListBox ItemsSource databinding issue (MVVM Pattern)

    I'm pretty new with WPF and the MVVM Framework, only about a week of playing with both.

    The problem I am running into is when binding to a list from my ViewModel. No matter what I do I cannot get the View to update the DataContext when an item is added, removed, or all items are cleared. I've played with the INotifyPropertyChanged while trying to figure out some other problems, but that does not work with collections. I read about INotifyCollectionChanged, but I can't seem to get that to work either.

    I wrote a sample project to display the error I am having. When you select an item and remove it, it will display how many items are currently in the list, then remove it, then how many are left. The listbox will not update, but the Refresh Items button will reset the DataContext and it will display properly.

    Quick rundown of the project:
    MainWindow, has a grid with a slot for my List view, (controller is created in code, then added to that slot in the constructor).

    The controller creates an instance of the view.

    View has an instance of the ViewModel, binds to the ItemsSource property of the list box.

    I created an IListViewItem interface with a Title, Description and ID, to make custom list items.

    ListViewItem implements the interface.

    MockListData generates 15 items and adds them to the ListViewModel.

    Sorry if I just confused anyone. Any help would be appreciated. Also, I am not too familiar with the MVVM Pattern, so if anyone notices I'm doing something completely wrong, feel free to point that out if you'd like.
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: ListBox ItemsSource databinding issue (MVVM Pattern)

    A good rule of thumb when working with lists is to use the observable collection interface as it has intrinsic support for updating etc. The other option is to implement commands, which honestly I have not had time to look at.

    Observable Collection: http://msdn.microsoft.com/en-us/library/ms668604.aspx

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Re: ListBox ItemsSource databinding issue (MVVM Pattern)

    I found the ObservableCollection a day or so after posting, but I'm still curious as to what I'm doing wrong. I do have a theory of what the problem could be though. I'm assuming that the reason the INotifyCollectionChanged isn't working is because I'm not creating a collection class. It's just a class that has a collection in it. I assume that if I were to create a custom collection and implement it in there, I could use my custom collection the same as an ObservableCollection.

    I haven't had the time to play with it yet, but it makes sense to me.

    Thanks for the link, I will check it out. I've read about commands, but as I just started a new job and got tossed into a project, so I've only learned the parts I've needed to as of yet. I'm loving WPF though, the more info the better.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width