Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Say I have the property
C#
private string _lastName;
[Required]
[StringLength(20)]
public string LastName
{
      get {return this._lastName;}
      set
      {
           if(value != this.-lastName)
           {
                ValidateProperty(value);
                this._lastName = value;
                RaisePropertyCanged("LastName");
           }
      }
}


The LastName comes from the textbox in a WPF view, two way binding.
When I don't fill it in the textbox, it is null. The code never reaches the setter in property.

What I have tried:

Try to move the Validation method out of setter. Not sure!
Posted
Updated 4-Jan-18 9:55am
Comments
Maciej Los 4-Jan-18 13:49pm    
Are you talking about validation in Model or ViewModel?
Member 12658724 4-Jan-18 15:17pm    
ViewModel

1 solution

Maciej Los wrote:

Are you talking about validation in Model or ViewModel?
Member 12658724 wrote:

ViewModel


So, you have to show us how View is related with ViewModel. In other words, what NotificationEvent fires ViewModel...

At this moment i'd recommend you to read few interesting articles:
The MVVM Pattern[^]
Windows Validation in MVVM using Data Annotations sample in C# for Visual Studio 2012[^]
Data validation in WPF | Magnus Montin[^] - recommeded for start!
Validating User Input - WPF MVVM[^]
MVVM Data Validation[^]
MVVM Validations[^]
WPF MVVM Validation ViewModel using IDataErrorInfo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900