I have a dataGrid, that uses an observableCollection to bind the ItemsSource property (MVVM pattern). So in my viewmodel I have a property that is an observableCollection (myCollection). However, this dataGrid can show information of two different types, that their are decide in runtime.
Normally, I use the the observableCollection in this away:
ObservableCollection<myType> myCollection = new ObservableCollection<myType>();
But now, I have a string as parameter that say me what type I need, so I would like to do something like that:
if(parameter == "TypeA")
{
myCollection = new ObservableCollection<TypeA>();
}
if(parameter == "TypeB")
{
myCollection = new ObservableCollection<TypeB>();
}
is it possible to do that?
If you make TypeA and TypeB derived from a common base class or interface, you can keep the same collection.
But if you want two different collections, you you can raise the collection property to notify about the changed type.
So your bind MyCollection in your view to ItemsSource, and raise that the property has changed.
Remember that you might need a different DataTemplate, where a DataTemplateSelector might come in handy.