Such a dilemma: I have a "ItemsSource" in my control. IEnumerable DependencyProperty . An ObservableCollection<T> attached to it. I need to add / remove items from the outside into the bound collection. However, I do not know which collection is bound, I have access only to the property of the element, and IEnumerable does not imply the Clear or Add functions.
What can be done here?
ICollection<T>, if you knowT, or even immediately toObservableCollection<T>. So many classes working with WPF do. - MonkIEnumerableproperty, the client has the right to write there any implementation, including those that do not support adding elements. If you need the ability to add elements, declare a type that supports this, for exampleIList. - Andrey NOPItemsControlis implemented - it also implements the separateItemsproperty. Well, you can see how it is done inDataGrid- if a collection is attached to it that does not allow adding elements, then this functionality is disabled, you can go along this path - Andrey NOPObservableCollection. I just don’t know the type of the element, so I made a genericIEnumerable. - Puro