Associate a WPF TreeView with a Common View Model

I have a usercontrol that contains a TreeView control. I am using the MVVM pattern. I want to reuse this user control in different windows, each time binding the usercontrol to another datacontext.

   <UserControl Name="UserControl1".......>

      ..............

        <TreeView ItemSource={Binding ...}...>


             <HierarchicalDataTemplate...........\>

        </TreeView>

      .............. 

   </UserControl>

In window 1, I want to bind List<ObjectA>to a TreeView.

In window 2, I want to bind List<ObjectB>to a TreeView.

Is it possible to write a generic ViewModel for this user control so that I can bind different data types to a TreeView ?? If my question is not understood, please let me know.

+3
source share
1 answer

, UserControl, , DataContext ViewModel ...

, , , ItemsSource TreeView, :

  • ViewModel
  • DataTemplate ( HierarchicalDataTemplate) , TreeView xaml
  • , ObservableCollection<T> , List<T>, / / , .

, :)

+2

Source: https://habr.com/ru/post/1725519/


All Articles