Background: I'm Using C#, WPF and Calburn.Micro to create a GUI-oriented application with a pre-defined set of polymorphic objects achieved by sub-classes inheriting from an abstract parent-class.
The user instantiates the different sub-classes using a form, which contains a combobox to select which class to instantiate (The Combobox SelectedItem is also planned to trigger changes the form contents according to the selected type's properties).
I've managed to bring all inheriting sub-classes into a BindableCollection by using:
private void LoadSubClasses()
{
Type _parentClass = typeof(ParentClass);
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] _types = assembly.GetTypes();
IEnumerable<Type> SubClassesIEnumerable = _types.Where(t => t.IsSubclassOf(_parentClass));
foreach (var _subclass in SubClassesIEnumberable)
{
SubClassesColletion.Add(_subclass);
}
}
private BindableCollection<Type> _subClassesCollection = new BindableCollection<Type>();
public BindableCollection<Type> SubClassesCollection
{
get { return _subClassesCollection; }
set { _subClassesCollection = value; }
}
In runtime I can see the Combobox populated by blank items (and the different collections in the debugger) which makes me thing that so far so good.
As I've stated before, some of the form contents will be changing according to the Combobox's SelectedItem, so I think the right direction is to have the Combobox item be the type, and then bind the different elements to the selected type's properties. I'm trying to implement that using ItemsTempalte, starting with the "subclass selector" Combobox displaying an abstract property (called "ViewName") which is overridden by all subclasses:
<ComboBox x:Name="SubClassesCollection" Height="23" Margin="5,0,5,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=ViewName}" Margin="-5"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But I fear this is as far as I can go, as looking up different solutions didn't get me anywhere.