I’m creating a UserControl which will be used in various scenarios. I need to expose a collection of strings from the UserControl and I’m not sure how to do it.
The two possible uses I see are:
- a control on its containing control binds to the collection, e.g. a ListBox;
- a property on the containing control’s ViewModel needs to bind to the collection.
I can get the former to work with a public ObservableCollection<String> property on the UserControl but it won’t work for the latter. (I get “Object of type ‘System.Windows.Data.Binding’ cannot be converted to type ‘System.Collections.ObjectModel.ObservableCollection`1[System.String]’.”)
Is there a solution that will work for both?
EDIT
This is my stab at a UML diagram showing what I’m doing:

I think the problem is just that the binder can’t understand generics. You should be able to get around that by inheriting from
ObservableCollection<string>to make a non-generic class. You can use something like this:Since you are using that property as a target of a binding, you must declare it as a DependencyProperty: