Hello!

You need to implement a thing in the program that is similar to the Visual Studio toolbar. That is, for example, when I select the Button in the object tree, the properties panel displays some parameters; if I select ListBox, they are completely different. And there are a lot of such objects with unique parameters.

That is, I need that when one object is selected, there are 3 buttons on this “panel with properties”, 2 text blocks, and in another case a different number of these elements.

How is this implemented?
Create for each object from the tree UserControl which will be the same panel with the properties?

  • one
    Maybe better to start with the finished. Here, for example, wpftoolkit.codeplex.com/… (there are a lot of such). In general, google "WPF Property Grid" - vitidev

1 answer 1

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}"/> 
  • For some reason, there are errors in the XAML markup: it says that it does not see Object1 and Object2. Can you throw off the finished project using this code? - trydex
  • @maxwell, I currently have no opportunity to throw off. Concerning errors, you added the namespace local where the classes Object1 Object2 ? - Gardes
  • Yes, he added. If it does not make it difficult to look at my code in the starting post. - trydex
  • @maxwell, updated the answer - Gardes
  • So it works. Thank! - trydex