Reputation: 5002
I am using MVVM using GalaSoft light Framework(mvvmlight(wpf4)). I have problems Validating Data input from the user and Displaying MessageBox indicating empty/incorrect input from the user,I wanted to use INotifyDataErrorInfo
and this article , but don't support in wpf4.
private string _password;
private string _userName;
[Required(AllowEmptyStrings = false, ErrorMessage = "Username is required")]
public string UserName
{
get { return _userName; }
set
{
if (_userName != value)
{
// ValidateProperty("UserName", value);
_userName = value;
base.RaisePropertyChanged("UserName");
}
}
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
public string Password
{
get { return _password; }
set
{
if (_password != value)
{
// ValidateProperty("Password", value);
_password = value;
base.RaisePropertyChanged("Password");
}
}
}
How to use Messanger
for input validation in mvvm light ?(send a message from save button to the ViewModel to check the input values.)
Upvotes: 0
Views: 3463
Reputation: 9242
you can send message on button.click event like this..
Messenger.Default.Send<string>("showattraction", "attraction");
first string is message and other is token for varifying it..
and where you recive this message do this..in constructor..
Messenger.Default.Register<string>(this, "attraction", GetLineDetails);
do your work in GetLineDetails method..
public void GetLineDetails(string Message)
{
// work here
}
Upvotes: 1
Reputation: 1474
Thought I would add how I have pulled off input validation (model validation not just input) with MVVM light and the Fluent Validation framework. It has worked out well for me and allows you to attached "IsValid" to your model so you can validate at any time.
I wrote a detailed blog post about it. MVVM Light and Model Validation
While it is true MVVM light itself does not have anything I wanted to provide a possible approach for others trying to pull this off. I have tried approaches with INotifyDataErrorInfo and others that are out there and found them lacking a bit. Hope it helps someone.
Upvotes: 2