How to add items after setting itemsource in ListView

I have an exception: Operation is not valid while using ItemsSource. Instead, select and modify items using ItemsControl.ItemsSource.

+4
source share
1 answer

You must add items to the original collection, which is set to ItemsSource . After you set the ItemsSource value to a value, it is impossible to use Items -property (in addition, you set ItemsSource new to null). Items and ItemsSource are mutually exclusive.

If you use ItemsSource , your source collection must implement INotifyCollectionChanged to report changes to the ListView . If not, the added items to it will not change the ListView . For instance. if you use List<T> as items-source, the changes will not be redirected.

There are classes that do this for you, such as ObservableCollection<T> .

+7
source

All Articles