3
votes

i have a container such as a listbox, combobox etc that its ItemsSource property is bound to an observable collection in my view model. When i'm trying to add/remove items from the collection via some method in my VM it won't reflect in the UI, The only way the UI would actually refresh is if i assign the collection a new value (i.e another collection with the relevant data) which forces him to re-bind the whole collection.

maybe i'm missing/don't understand something about the collection binding issue, either way if someone has a solution/good explanation/both it would be great. here is a sample from my View(in this case its a listbox)

<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.

2
Show your code attempts so farSheridan
just for better understanding - the initial values in your collection are shown in your listbox?blindmeis
yes, initial values are shown. i can for example add a few items in the C'tor and they will be visible to the user, but any change i'm doing after(add/remove/clear etc.) is not refreshing the UIuser1531186
pls post the code where you set the datacontext too and ckeck the datacontext and binding with snoop on runtime.blindmeis

2 Answers

0
votes

viewmodel

public ObservableCollection<yourType> MyItemsSource {get;set}

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

view

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

just be sure that the right DataContext is set.

thats how it should look in your code

EDIT: some hints to your posted 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 in all your code looks good, and if the viewmodel is the Datacontext of your Listbox then it should work. let me know what Snoop is showing :)

0
votes

Remove the OnPropertyChanged("PreSavedRecordingScheduleList"); from the ObservableCollection. Actually you don't need a backing field. Attach the CollectionChanged event on the ObservableCollection, something like this

1- Inside the ViewModel constructor attach the event CollectionChanged

PreSavedRecordingScheduleList = new ObservableCollection<ScheduledRecordingObject>();

PreSavedRecordingScheduleList.CollectionChanged += PreSavedRecordingScheduleList_CollectionChanged;

2- Inject the OnPropertyChanged("PreSavedRecordingScheduleList") in the event handler

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