I’m learning MVVM pattern with C#, WPF and .NET Framework 4.5.1.
I’m using MVVM Light framework to do it and now I have a doubt. Reading the book Windows 8 MVVM Patterns Revealed I see that the author uses Event Aggregator Pattern
.
I’m not sure if I need it, because in the following ViewModel
I would have to raise the event to EventAggregator
and handle it on the same ViewModel
.
Here is my ViewModel
:
public class MainViewModel : ViewModelBase
{
private RelayCommand doLoginCommand;
/// <summary>
/// The <see cref="UserName" /> property's name.
/// </summary>
public const string UserNamePropertyName = "UserName";
private string _userName = null;
/// <summary>
/// Sets and gets the UserName property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string UserName
{
get
{
return _userName;
}
set
{
if (_userName == value)
{
return;
}
RaisePropertyChanging(UserNamePropertyName);
_userName = value;
RaisePropertyChanged(UserNamePropertyName);
}
}
/// <summary>
/// The <see cref="UserPassword" /> property's name.
/// </summary>
public const string UserPasswordPropertyName = "UserPassword";
private string _userPassword = null;
/// <summary>
/// Sets and gets the UserPassword property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string UserPassword
{
get
{
return _userPassword;
}
set
{
if (_userPassword == value)
{
return;
}
RaisePropertyChanging(UserPasswordPropertyName);
_userPassword = value;
RaisePropertyChanged(UserPasswordPropertyName);
}
}
public RelayCommand DoLoginCommand
{
get { return doLoginCommand; }
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
////if (IsInDesignMode)
////{
//// // Code runs in Blend --> create design time data.
////}
////else
////{
//// // Code runs "for real"
////}
this.doLoginCommand = new RelayCommand(ExecuteDoLogin);
}
private void ExecuteDoLogin()
{
Debug.WriteLine(_userName);
}
}
And its XAML:
<Window x:Class="WPFMvvmApress.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">
<Grid>
<StackPanel x:Name="loginPanel" HorizontalAlignment="Center" Height="159" Margin="0" VerticalAlignment="Center" Width="349">
<TextBox
x:Name="userName"
HorizontalAlignment="Left"
Height="23"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="231"
Margin="10,10,0,5"
Text="{Binding Path=UserName, Mode=TwoWay}"/>
<TextBox
x:Name="userPassword"
HorizontalAlignment="Left"
Height="23"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="231"
Margin="10,5,0,5"/>
<Button
x:Name="loginButton"
Content="Login"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="75"
Margin="10,5"
Command="{Binding Path=DoLoginCommand}"/>
</StackPanel>
</Grid>
</Window>
What do you think?
4
The general idea of MVVM is to use the ViewModel to model the View so that the ViewModel data-binds to elements on the View which usually includes the Model (for more information, see my answer to this question: How do I know if I’m violating MVVM with WPF?).
The event aggregator or mediator is used to transmit events between the layers that should not be aware of each other. In your example above, data-binding to a command on the ViewModel is the preferred way to handle events that can be bound to a command.