WPF MVVM ListBox / ComboBox ItemsSource will not update user interface from ViewModel

I have a container, such as listbox, combobox, etc., that its ItemsSource property is bound to an observable collection in my view model. When I try to add / remove elements from the collection using some method in my virtual machine, it will not be reflected in the user interface.The only way the user interface will update is if I assign a new value to the collection (i.e. Another collection with the corresponding data), which forces it to re-bind the entire collection.

Maybe I am missing / do not understand something about the problem with binding to the collection, anyway, if someone has a solution / good explanation / both options will be great. here is a sample from my view (in this case, its list)

<ListBox
                Grid.Row="9"
                Grid.Column="1"
                Grid.ColumnSpan="3"
                Width="200"
                Height="200"
                ItemsSource="{Binding PreSavedRecordingScheduleList,UpdateSourceTrigger=PropertyChanged}"
                SelectedItem="{Binding SelectedPreSavedRecordingSchedule,UpdateSourceTrigger=PropertyChanged}"
                DisplayMemberPath="Display"/>

and here is my ViewModel:

private ObservableCollection<ScheduledRecordingObject> m_PreSavedRecordingScheduleList;

PreSavedRecordingScheduleList = new ObservableCollection<ScheduledRecordingObject>();

public ObservableCollection<ScheduledRecordingObject> PreSavedRecordingScheduleList
       {
            get
            {
                return m_PreSavedRecordingScheduleList;
            }
            set
            {
                m_PreSavedRecordingScheduleList = value;
                OnPropertyChanged("PreSavedRecordingScheduleList");
            }
        }

ScheduledRecordingObject also implements INotifyPropertyChanged.

+3
source share
2 answers

ViewModel

public ObservableCollection<yourType> MyItemsSource {get;set}

initialize once in the contructor and use clear, add and remove to change it

view

 <ListBox ItemsSource="{Binding MyItemsSource}"/>

just make sure the correct DataContext is set.

this is how it should look in your code

EDIT: some tips for your published code:

//remove the UpdateSourceTrigger=PropertyChanged - makes no sense the Mode is OneWay anyway :)
ItemsSource="{Binding PreSavedRecordingScheduleList}"

//the following line should just called once and at best in ctor
//but the binding will of course work too when you assign a new collection
PreSavedRecordingScheduleList = new ObservableCollection<ScheduledRecordingObject>();  

all your code looks good, and if the viewmodel is the datacontext of your list, then it should work. let me know what Snoop shows :)

0

OnPropertyChanged ( "PreSavedRecordingScheduleList" ); ObservableCollection. . CollectionChanged ObservableCollection, -

1- ViewModel CollectionChanged

PreSavedRecordingScheduleList = new ObservableCollection<ScheduledRecordingObject>();

PreSavedRecordingScheduleList.CollectionChanged += PreSavedRecordingScheduleList_CollectionChanged;

2- OnPropertyChanged ( "PreSavedRecordingScheduleList" )

void PreSavedRecordingScheduleList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged("PreSavedRecordingScheduleList");
    }
0

All Articles