Closed ghost closed 8 years ago
@thoemmi Please bring updates to TinyLittleMvvm Its very helpful
TinyLittleMvvm includes the class ValidationPropertyChangedBase
, inherited from PropertyChangedBase
. Does it provide the functionality you're asking for?
@thoemmi yes it does but not sure how it should work can you assist me,
My above method is a override method so in my SimcardViewModel.cs I should use ValidationPropertyChangedBase instead of PropertyChangeBase
Please see my file below and give me a helping hand SimCardViewModel.zip
Hi @thoemmi, did you manage to have a look at my SimCardViewModel
For an example of ValidationPropertyChangedBase
see the demo application in this repository. Especially have a look at SampleDialogViewModel.cs.
Some notes about your SimCardViewModel
:
You can simplify your calls of NotifyOfPropertyChange
as you can pass an expression too. i.e. instead of writing
NotifyOfPropertyChange(GetPropertyName(() => MSISDN));
call
NotifyOfPropertyChange(() => MSISDN);
And IsValid
can be changed to
public bool IsValid { get { return !HasErrors; } }
@thoemmi Thank you so much sorry to pick your brain but below should I take this out?
/// <summary>
/// Gets the error string for a property value against a property's name.
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
protected override string OnErrorsChanged(string propertyName)
{
string error = null;
switch (propertyName)
{
case "ICCID":
if (string.IsNullOrWhiteSpace(ICCID))
error = "ICCID is mandatory.";
else
{
if (null != ICCID && ICCID.Length > 20)
error = "Max 20 chars permitted.";
}
break;
case "IMSI":
if (IMSI.Length > 15)
error = "Max 15 chars permitted.";
break;
case "MSISDN":
if (MSISDN.Length > 15)
error = "Max 15 chars permitted.";
break;
}
CommandManager.InvalidateRequerySuggested();
return error ?? Entity[propertyName];
}
I don't think that your code is necessary. Instead, you can add your validation rules in the constructor of SimCardViewModel
:
public SimCardViewModel()
{
AddValidationRule(() => ICCID, text => !String.IsNullOrEmpty(text), "ICCID is mandatory.");
AddValidationRule(() => ICCID, text => !String.IsNullOrEmpty(text) && text.Length <= 20, "Max 20 chars permitted.");
AddValidationRule(() => IMSI, text => !String.IsNullOrEmpty(text) && text.Length <= 15, "Max 15 chars permitted.");
AddValidationRule(() => MSISDN, text => !String.IsNullOrEmpty(text) && text.Length <= 15, "Max 15 chars permitted.");
}
@thoemmi Thank you I appreciate the help :+1:
Hi @thoemmi
Was wondering can you add the
GetErrorForProperty
method toPropertyChangedBase
Like this https://github.com/OxyProgrammer/XPence/blob/master/WPF%20Client/XPence/XPence.Infrastructure/BaseClasses/ViewModelBase.cs
So I can use it in my project
If that makes any sense sorry still beginner dev