简体   繁体   中英

How to navigate between different nested ViewModels?

I have a ViewModel Call Apple which has AppleItems. In the UI of the Apple ViewModel there is a nested ViewModel called AppleNote which has AppleNote items. When I am on the UI of the Apple ViewModel, there is an icon which lets me navigate into the AppleNote viewmodel. The way I am doing it is using eventagregators show in the code below. It is using binding as a DelegateCommand. My question is that when I am on Item#3 or any other item of AppleItems in Apple ViewModel and when I hit AppleNote for that Item it leads me into the right viewModel but when I want to switch back to the AppleItem view of that viewModel, I just get to the first item in the AppleItems. How can I tell that when going back to the AppleItems viewModel keep the item where you came from. If there is any confusion let me know so I can make it clear.

Here is a summary:

AppleViewModel-->Has-->AppleItems

             |

            Has--> AppleNoteViewModel-->Has-->AppleNotesItem

Going for a certain AppleItem to AppleNote is working but going back to the certain item is not working.

   private void AppleCommandExecute(object commandParameter)
    {

          _eventAggregator.Publish
            (new ChangeValueViewEventArgs
            {
                ViewModelType = typeof (AppleViewModel),


                Data = ValueId.ToString()
            });
    }

As I implement the INotifyPropertyChanged interface directly in my data classes, I don't have any nested view models, but I do have nested, or hierarchical data classes. In my base collection class, I have a property named CurrentItem of the same type as the items in the collection.

When I bind one of these collections to the ItemsSource property of a collection control, I also bind the CurrentItem property to the SelectedItem property of the control. This essentially means that I always know which is the current item in each collection.

If you implement one of these CurrentItem properties then you will always know which AppleItem or AppleNote is selected. You don't have to put these properties into collection classes... you could just add properties of the correct type to your view models instead. The point is that if you always bind to the SelectedItem property, then you will always know which item is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM