I created a treeview in WPF MVVM with a hierarchical DAtaTemplate property.
Outside of the Treeview, I have an “EDIT” button. On press of edit button I want to pass the Treeview’s selectedItem to the RelayCommand in the viewmodel.
I am implementing the same functionality within the TreeviewItem on double click of the node. However I want to know what is the best way to accomplish this outside.
My code structure is as follows
MainView -> MainViewModel
StateViewItem -> StateViewModel (HierarchicalDataTemplate inheriting from TreeViewItemViewModel)
RegionViewItem -> RegionViewModel (DataTemplate inheriting from TreeViewItemViewModel)
The RegionViewModel handles the double click for the node and accomplishes the Edit functionality.
However the “EDIT” button is outside of the treeview, how do I pass the selected RegionViewItem to the Command bound so I can load it for editing?
thanks in advance for help
Does the
RegionViewModelhave access to theStateViewModel? Or more importantly, toStateViewModel.SelectedItem? (Some code would be helpful)If not, you may have to implement an event system where whenever
StateViewModel.SelectedItemchanges, it broadcasts aStateChangedevent, andRegionViewModelsubscribes to that event and keeps track of theSelectedState