I have the following situation:
Views:
- EbayCategoriesView (with a TreeView)
- MaintainEbayCategoryView (with the category detail fields)
ViewModels:
- EbayAllCategoriesViewModel (with ObservableCollection of EbayCategoryViewModel called EbayCategories).
- EbayCategoryViewModel(with Category Properties)
The EbayCategoryViewModel has a public Property, that is a List called ChildrenCategories. the TreeView is bound to the ObservableCollection and each node of the TreeView is an HyperLink. I would like that when the Hyperlink is clicked, the system opens the MaintainEbayCategoryView and load it with the EbayCategoryViewModel data.
I'm using the MVVM pattern; the EbayCategoryViewModel accept an input ICommand parameter in the Constructor. When I populate the ObservableCollection in the EbayAllCategoriesViewModel, I create a new RelayCommand for each element of the collection and pass the function that should be executed:
hierarchyList.Add(new EbayCategoryViewModel(
item.EbayCategoryName,
item.EbayCategoryID,
FillRecursive(flatList, item.EbayCategoryID, item.EbayCategoryName),
parentCategoryName,
item.EbayOrder,
new RelayCommand(cmd => this.LoadCategoryDetails())
));
My problem now is that in the LoadCategoryDetails() method of the EbayAllCategoriesViewModel I don't have any reference of the EbayCategoryViewModel tree node that has been clicked (the SelectedItem in the TreeView is not public, and also I'm not sure it contains the element being clicked...).
Even if I found some workaround, I would like to understand what is the correct approach to solve my problem, respecting the MVVM pattern. Since I already have all the category fields in the EbayCategoryViewModel, I would be able to access the current EbayCategoryViewModel object being clicked, without accessing again my source of data.
Thanks in advance for any suggestion.