You can select an abstraction of your objects. Create an abstract class type collection. Shoot a collection, for example, in ListBox , bind to SelectedItem , and in ContentControl already display the selected item, having predefined a DataTemplate for each object.
Example:
public abstract class AbstractCustomObject { public string Name { get; set; } }
VM:
public class MainViewModel : INotifyPropertyChanged { public ObservableCollection<AbstractCustomObject> MyObjects { get; set; } private AbstractCustomObject _selectedObject; public AbstractCustomObject SelectedObject { get { return _selectedObject; } set { _selectedObject = value; NotifyPropertyChanged("SelectedObject"); } } public MainViewModel() { MyObjects = new ObservableCollection<AbstractCustomObject>() { new Object1(){Name ="Объект 1"}, new Object2(){Name ="Объект 2"} }; } // реализация INotifyPropertyChanged // INotifyPropertyChanged - используется для уведомления представления об изменениях свойств объекта public event PropertyChangedEventHandler PropertyChanged; protected virtual void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
now we need to set the DataContext for our window: see how to do it here .
xaml markup:
<Window.Resources> <ResourceDictionary> <DataTemplate DataType="{x:Type local:Object1}"> <StackPanel> <TextBlock Text="{Binding Name}"/> <Button Content="button1"/> <Button Content="button2"/> <Button Content="button2"/> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type local:Object2}"> <StackPanel> <TextBlock Text="{Binding Name}"/> <TextBlock Text="{Binding }"/> <TextBlock Text="{Binding }"/> <Button Content="button2"/> </StackPanel> </DataTemplate> </ResourceDictionary> </Window.Resources> <ListBox ItemsSource="{Binding MyObjects}" SelectedItem="{Binding SelectedObject}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <ContentControl Content="{Binding SelectedObject}"/>