MVVM and Nested View Models - Link and Search Lists

I am using Prism for a new application I am creating. There are several search lists that will be used in several places in the application. So it makes sense to define it once and use it wherever I need this functionality. My current solution is to use typed data templates to render controls inside a content control.

   <DataTemplate DataType={x:Type ListOfCountriesViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem="{Binding SelectedCountry"/>      </DataTemplate>
   <DataTemplate DataType={x:Type ListOfRegionsViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem={Binding SelectedRegion} />   </DataTemplate>

    public class ParentViewModel
   {
        SelectedCountry get; set;
        SelectedRegion get; set;
        ListOfCountriesViewModel CountriesVM;
        ListOfRegionsViewModel RgnsVM;
   }

      

Then in my window I have 2 content controls and the rest of the controls

<ContentControl Content="{Binding CountriesVM}"></ContentControl>
<ContentControl Content="{Binding RgnsVM}"></ContentControl>
<Rest of controls on view>

      

At the moment I have this working and the SelectedItems for comboboxes post events via the EventAggregator from the child view models which are then subscribed to the parent view model.

I'm not sure if this is the best way to go as I can imagine that you end up with a lot of events very quickly and it will get cumbersome. Also, if I were to use the same ViewModel in another window, it would post an event and that parent model would subscribe to it, which could have unintended consequences.

My questions: -

  • Is this the best way to put search lists into a view that can be reused across different screens?
  • How to make combobox view associated with child model set corresponding property in parent model without using events / proxies. for example, in this case, for example, for the selected country?
  • Any alternative suggestions for implementing what I am trying to do?

I have a feeling that I am missing something obvious and there is so much information out there that it is difficult to figure out which is correct, so any help would be greatly appreciated.

+2


a source to share


1 answer


In your scenario, it appears that the parent knows the child's type. Can't you just use properties in this situation?

ListOfCountriesVM provides the SelectedCountry property.

In the parent virtual machine, the SelectedCountry implementation simply returns ListOfCountriesVM.SelectedCountry.

You can do this for any parent view model that knows it owns the ListOfCountriesVM.



In my experience, message / event aggregation should really only be used in scenarios where the publisher doesn't care who is listening or how many listeners there are. For instance:

  • Sending notifications to other modules in the app
  • Sending notifications to view models in the same module, which you cannot guarantee.

In your situation, the subscriber (parent) already knows who the publisher (child) is, so a simple solution using properties is most appropriate. I don't think this particular scenario requires a messaging or event solution.

+3


a source







All Articles