I'm not exactly sure if we're on the same page, but I'm assuming that your MainViewModel
class is like a 'base view model' class. I have a generic BaseViewModel
class in my MVVM projects, but I also have generic BaseDataType
classes as well that (both) expose the INotifyPropertyChanged
interface to the extending classes.
In my applications, I have a series of Manager
classes that implement the Singleton pattern (eg. there can be only one of each). The BaseViewModel
class exposes these valuable manager classes to the extending view model classes. Each manager class provides some further functionality. For example, see the following list:
StateManager: maintains global data/object state across the whole application
DependencyManager: maintains dependency with a collection of interfaces and their concrete implementations
FeedbackManager: maintains access to the application feedback control (for user feedback)
WindowManager: provides access to file dialogs and child window management
ClipboardManager: provides access to the computer clipboard
UiThreadManager: provides access to multi-threading
EmailManager: provide access to be able to send e-mails
HardDriveManager: provides access to the user's computer hard drives
ExportManager: provides access to XML generation and FTP transfers
DataOperationManager: provides access to all data operations (explained further below)
UpdateManager: provides access to application updates
SecurityManger: provides access to all security matters
ExcelManager: provides access to functionality that generates Excel documents based on view model data.
All of these manager classes are accessible from any view model that extends the BaseViewModel
class.
In addition to this, my (abstract) BaseViewModel
also exposes certain Command
objects that are required in every view, like 'Save', Delete', 'Refresh' etc.
Finally, it also provides access to often used functionality like 'InsertNewDataTypeToCollection' and 'RemoveDataTypeFromCollection' methods.
As well as this part of the 'framework', I also have a number of base data type classes. These provide common properties and often used functionality like data synchronisation, animation, data error reporting (an extension of the 'IDataErrorInfo' interface) which automatically links in with the feedback control.
The final part of my system revolves around database access. I have a suite of classes that I wrap around every data object that comes or goes to the database. These automatically provide user feedback in the UI, asynchronous operations, and error logging and handling.
I hope that this helps you and that I didn't misunderstand your question.
UPDATE >>>
Oooops, I forgot to mention the most important part... I have a MainViewModel
class that extends BaseViewModel
and is displayed in the MainWindow.xaml
. In this view model, there is a public property of type BaseViewModel
:
public BaseViewModel ViewModel
{
get { return viewModel; }
set
{
if (viewModel != value)
{
viewModel = value;
NotifyPropertyChanged("ViewModel");
}
}
}
I can set this property to any view model class that has extended the BaseViewModel
class. In MainWindow.xaml
, I have the following setup:
...
<Grid Grid.Row="1" Background="{StaticResource Windows7LightBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Grid.RowSpan="2" Content="{Binding ViewModel}" Margin="5" />
</Grid>
<Controls:FeedbackControl Grid.Row="0" Feedback="{Binding FeedbackManager.Feedback}"
VerticalAlignment="Top" HorizontalAlignment="Stretch" MaxWidth="750"
Margin="100,22,100,0" />
...
In the first Grid
row, I have a Ribbon
control, in the second I have a ContentControl
that displays the view that matches the view model that is set in the ViewModel
property and then I have a custom FeedbackControl
that slides in and out of view when feedback arrives.
The final piece of this puzzle is linking the views with the view models. This is done in the App.xaml
file using DataTemplate
objects:
...
<DataTemplate DataType="{x:Type ViewModels:HomeViewModel}">
<Views:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
<Views:MainView />
</DataTemplate>
...