Is it true that if I create a ViewModel I need to create Model for it in MVVM pattern?
For example my task to create simple Menu in WPF app at the top of the window.
This is my View MainWindow.xaml
<Window x:Class="AppMvvm.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AppMvvm.ViewModel"
Title="{Binding DisplayName}" Height="350" Width="525"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<Style x:Key="MenuItemStyle">
<Setter Property="MenuItem.Command" Value="{Binding Command}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type vm:MenuItemViewModel}"
ItemsSource="{Binding Children}">
<ContentPresenter Content="{Binding DisplayName}" />
</HierarchicalDataTemplate>
</Window.Resources>
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<Menu ItemsSource="{Binding MenuItems}" ItemContainerStyle="{StaticResource MenuItemStyle}"/>
</DockPanel>
</DockPanel>
</Window>
Here is my ViewModel for MenuItemViewModel.cs
namespace AppMvvm.ViewModel
{
public class MenuItemViewModel : ViewModelBase
{
public MenuItemViewModel(string menuItemName, IList<MenuItemViewModel> children)
{
base.DisplayName = menuItemName;
this.Children = children;
}
public MenuItemViewModel(string menuItemName)
: this (menuItemName, children: null)
{
}
public MenuItemViewModel(string menuItemName, ICommand command)
{
base.DisplayName = menuItemName;
this.Command = command;
}
public IList<MenuItemViewModel> Children { get; private set; }
public ICommand Command { get; private set; }
}
}
My class WorkspaceViewModel.cs that is base class for MainWindowViewModel.cs which is binded to the View
namespace AppMvvm.ViewModel
{
using Helpers;
public abstract class WorkspaceViewModel : ViewModelBase
{
#region Fields
private IList<MenuItemViewModel> _menuItems;
private RelayCommand _closeCommand;
#endregion
#region MenuItems
public IList<MenuItemViewModel> MenuItems
{
get
{
if (_menuItems == null)
_menuItems = CreateMenuItems();
return _menuItems;
}
}
List<MenuItemViewModel> CreateMenuItems()
{
return new List<MenuItemViewModel>
{
new MenuItemViewModel("File", CreateFileMenuItems()),
new MenuItemViewModel("Tools"),
new MenuItemViewModel("About")
};
}
#region CreateFileMenuItems & Commands
List<MenuItemViewModel> CreateFileMenuItems()
{
return new List<MenuItemViewModel>
{
new MenuItemViewModel("New"),
new MenuItemViewModel("Exit", CloseCommand)
};
}
public ICommand CloseCommand
{
get
{
if (_closeCommand == null)
_closeCommand = new RelayCommand(p => Close());
return _closeCommand;
}
}
void Close()
{
Application.Current.Shutdown();
}
#endregion
#endregion
}
}
Is it correct to do it in this way or I need to create a Model for the MenuItemViewModel class and do it another way?
Yes and no.
The MVVM pattern by its very nature stipulates that you will have a Model, but this doesn’t mean that you need to fully implement MVVM.
Exactly what you decide to do will depend on your project, and your understanding (or interpretation) of MVVM. I’ve taken an approach before that was a combination of MVVM with MVC – i.e. it had a V/VM and then a controller sitting behind it (the controller is almost like a super duper model). If you have a very simple application then there is no point making it complicated simply for the sake of pattern purity – stick with just a view and viewmodel if that suits your purposes.
The thing to remember is that a pattern is simply a recipe or prescription of how to do something. There is no rule that says you should adhere to it rigidly, in many cases you may find that you can’t and you therefore either have to adapt a pattern or use several patterns – the key thing is to maintain consistency with what you do.