2

I'm new with MVVM and I'm stuck...

I have a ListBox in the MainWindow. The ListBox contains Items of type WhatEverViewModel which are displayed by DataTemplates. The user can interact with these items and the WhatEverViewModel has several DependencyProperties which may change during interaction.

The question I have is: How can I elegantly react (in the MainWindowViewModel) to changes of DependencyProperties of the CURRENTLY SELECTED WhatEverViewModel. I personally would implement some events in the WhatEverViewModel and when the SelectedItem of the ListBox change, I would attach to the events of the currently selected WhatEverViewModel. But I think in MVVM there might be a more elegant way to solve this...

Thank you.

  • Are you using thv view-first or the view-modelfirst approach? – PVitt Apr 26 '12 at 09:35
  • Why do you have a listBox containing a list of ViewModels? You would normally have 1 viewModel per 1 view and each viewModel would have a list of Models (for example) which you bind the listbox to in the view. Unless this is what you meant. – HAdes Apr 26 '12 at 10:21
  • The viewmodel has a list of viewmodels which are bound to the listbox in xaml. – JensPfister1 Apr 26 '12 at 10:56

3 Answers3

2

Make CurrentWhatEver a property of your MainWindowViewModel and bind the Listbox.SelectedItem property on it. This way, MainWindowViewModel knows when the selected WhatEver changes and can register/unregister to events it's interested in.

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
  • That's what I already did. So there is no other solution than a event in the WhatEverViewModel that notifies when an important property changed? – JensPfister1 Apr 26 '12 at 09:51
  • Maybe you can explain in more details what is happening to your WhatEverViewModel and how you want your MainWindowViewModel to reacts. – Nicolas Repiquet Apr 26 '12 at 09:58
  • The WhatEverViewModel has a list which is also bound to a listbox (in datatemplate) and depending on what I select in the WhatEverViewModel I want to display some kind of "Configurator" in the MainViewModel. – JensPfister1 Apr 26 '12 at 10:57
2

communication between viewmodel can be done in several ways.

  • Messenger/Mediator like the one form MVVM Light
  • Eventstuff like the one from PRISM
  • or simply use harcoupling and subscribe to the events from the WhatEverViewModel in your mainviewmodel.

btw why in hell to you use DependencyProperties in your Viewmodels? simple Properties with INotifyPropertyChanged are the way to go.

one more thing. why you want to react to changes in the SelectedViewmodel(or better what you want to achieve, with the selected viewmodel.)? if you just want to display some information in your view, simply bind the SelectedViewmodel to it. you should specify your question in that way.

EDIT

The WhatEverViewModel has a list which is also bound to a listbox (in datatemplate) and depending on what I select in the WhatEverViewModel I want to display some kind of "Configurator" in the MainViewModel. – JensPfister1 1 hour ago

why not simply bind the SelectedWhatEverViewmodel.SelectedListEntryProperty to your configurator view? can you post some code?

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • I can manipulate the items of the listbox within the UI. And I want to react if some properties of the currently selected item are about to change. Why not use DependencyProperties? – JensPfister1 Apr 26 '12 at 10:53
  • because simple properties and INotifyPropertyChanged do the job. – blindmeis Apr 26 '12 at 12:45
0

You should implement the INotifyPropertyChanged interface on each of your ViewModels. Then when one of your properties changes call the PropertyChanged event, your views will get notifications that the property has changed (as long as your Binding is correct). If a property is a list or collection make sure that the list is based off an INotifyCollectionChanged.

Add a property for the Selected WhatEverViewModel to your MainWindowViewModel, bind that in your ListBox. Then in your MainWindowViewModel you can hook on to the property changes of your Selected WhatEverViewModel.

For more guidance read:

Community
  • 1
  • 1
Kolky
  • 2,917
  • 1
  • 21
  • 42