Question

My application is for medical clinics and I select a patient and then there are several Tabs to choose from, Patient Info, Family History and so on. I started out with all tabs in the MainView but have come to the conclusion that each tab needs to be on separate Views because of the complexity of the screens.

How then do I share the Patient info across multiple views? All screens relate to the Patient in some way and I have gone down the road of MainViewModel hosting all the data but that seems like a bad idea because I manipulate data on separate views and need to get the updates back to the MainViewModel. What is the right approach, I have searched the internet for days looking at different architecture and had little to no luck finding any type of similar architecture.

What I did so each ViewModel could access the Patient data is use the code below.

        <TabControl Grid.Row="2" x:Name="TC">

        <TabItem Header="Patient Info" IsEnabled="{Binding IsPatientSet}" DataContext="{Binding}">
            <view:TabPatientView DataContext="{Binding ElementName=TC, Path=DataContext}" />
        </TabItem>

        <TabItem Header="Clinical Worksheet" IsEnabled="{Binding IsPatientSet}">

I bound each ViewModel to the MainViewModel since each ViewModel was just one aspect of Patient Info. Works great!

Was it helpful?

Solution

The architecture of a program depends on different varibles and there is no one correct answer which I can give you. However, I can propose some ways to handle your issue:

  1. Keep a Master ViewModel (which is what you are doing now). Create a ViewModel for each View and use a common class (e.g Service) to share patient info between the View models. In this case, service may also expose an event which can be raised when patient info is changed.

  2. Use a light weight WPF infrastructure like MVVM Light Toolkit to share changes in patient info by using the Messenger instance. (Like option 2, but using free 3rd party infrastructure)

  3. Use PRISM with DI.

If all of yours View-ViewModel code resides in the same DLL, you might consider using option 2, or 3. If some of the View-ViewModel resides in different assemblies you might consider using option 4 which presents the EventAggregator design pattern.

Hope this help to get you on the right path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top