将itemsource设置为ListView后如何添加项目

时间:2022-04-12 07:10:14

I got exception : Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

我遇到异常:当ItemsSource正在使用时,操作无效。使用ItemsControl.ItemsSource访问和修改元素。

1 个解决方案

#1


7  

You must add the items to your source-collection that is set to ItemsSource. After you have set the ItemsSource-property to a value, its no more possible to use the Items-property (besides you set ItemsSource newly to null). Items and ItemsSource exclude each other.

您必须将项添加到设置为ItemsSource的源集合中。将ItemsSource属性设置为值后,不再可能使用Items-property(除了将ItemsSource新设置为null)。 Items和ItemsSource相互排斥。

If you use ItemsSource, your source-collection must implement INotifyCollectionChanged so that it will inform the ListView about changes. If not, added items to it will not change the ListView. E.g. if you use List<T> as items-source, changes will not be forwarded.

如果使用ItemsSource,则源集合必须实现INotifyCollectionChanged,以便它将通知ListView有关更改。如果没有,添加项目将不会更改ListView。例如。如果您使用List 作为项目来源,则不会转发更改。

There exist classes that do that for you such as ObservableCollection<T>.

存在为您执行此操作的类,例如ObservableCollection

#1


7  

You must add the items to your source-collection that is set to ItemsSource. After you have set the ItemsSource-property to a value, its no more possible to use the Items-property (besides you set ItemsSource newly to null). Items and ItemsSource exclude each other.

您必须将项添加到设置为ItemsSource的源集合中。将ItemsSource属性设置为值后,不再可能使用Items-property(除了将ItemsSource新设置为null)。 Items和ItemsSource相互排斥。

If you use ItemsSource, your source-collection must implement INotifyCollectionChanged so that it will inform the ListView about changes. If not, added items to it will not change the ListView. E.g. if you use List<T> as items-source, changes will not be forwarded.

如果使用ItemsSource,则源集合必须实现INotifyCollectionChanged,以便它将通知ListView有关更改。如果没有,添加项目将不会更改ListView。例如。如果您使用List 作为项目来源,则不会转发更改。

There exist classes that do that for you such as ObservableCollection<T>.

存在为您执行此操作的类,例如ObservableCollection