Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i am fresher to WPf concept. i want to do a project based on "employement registration".this work needs the input fields name,age,depatrment,designation,salary.these details should be saved in sql database only after the validation. the name field should only contains alphabets and age field should contain only numbers.can we use mvvm to this??how can i implement this using mvvm concept?
Posted 17-Dec-12 19:53pm
Anmerin317
Edited 17-Dec-12 23:32pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi , check here.
Obviously for simplicity your domain model -> Employee needs to inherit
IDataErrorInfo

then implement in this domain model property (which is a declared in this interface):
public string this[string columnName]
and internally check your fields there , in case of inproper data for each property notify about it in format of string data.
Also you can check here:
idataerrorinfo-for-validation-in-mvvm-with-silverlight-and-wpf/[^]
  Permalink  
v2
Comments
Anmerin at 18-Dec-12 22:33pm
   
thanks for ur response.i cant understand the mvvm model.i came to know that there is some divisons like view,model,model view and we have to create separate classes for each one except the view which is our xaml part.and each class is having some functionality like properties should be implemented in one class all the actions should be implemented in another class which also uses inotify proerty.will u pls give me brief skeleton structure(coding part)???
Oleksandr Kulchytskyi at 19-Dec-12 3:35am
   
Ok.
Well, personally for me,it would be preferable to work in Wpf MVVM with conjunction of third-party Library called MvvmLight (Galasoft.MvvmLight.* namespaces), by its convenience and exposing many helpfull features like Mediator , Notification, Threading. You can get it via NuGet package manager.
 
So let begin with our domain model.
//as far as you want to perform various validations against model, our domain model inherits IDataErrorInfo interface
public class PersonModel: GalaSoft.MvvmLight.ObservableObject,IDataErrorInfo
{
private int _id;
public int Id
{
get{return _id;}
set { if(_id!=value){_id=value; base.RaisePropertyChanged(()=>Id);}}
}
... other properties was omitted for brevity
}
 
ViemModel part:
 
public sealed class PersonViewModel:GalaSoft.MvvmLight.ViewModelBase
{
private PersonModel _curPer;
public PersonModel CurrentPerson
{
get{return _curPer;}
set
{
_curPer=value; base.RaisePropertyCahnged(()=>CurrentPerson);
}
}
 
private ObservableCollection<PersonModel> _perList;
public ObservableCollection<PersonModel> PersonList
{
get{return _perList;}
set
{
_perList=value; base.RaisePropertyCahnged(()=>PersonList);
}
}
// other stuff(Commands and so on) was omitted for brevity.
}
 
And last , you xaml:
 


....

<ListBox ItemsSource="{Binding PersonList}" DisplayMemberPath="Id"
SelectedItem="{Binding CurrentPerson,Mode=TwoWay}"/>
Anmerin at 27-Dec-12 5:56am
   
thnkz for ur response.Hw can we add validation to the property name and age.?
 
The name field should only accept alphabets and age field should be of numerical .
 
Can we use the following logic?/or is der any other logic??
 
if (!Regex.IsMatch(FirstName, "^[a-zA-Z ]+$"))
{
result = "Name doesnot contain Numerical value";
}
 

The class in which am doing the validation is also pasting here
 

public class Customer : IDataErrorInfo
{
private string name;
public string FirstName
{
get
{
return name;
}
set
{
name = value;
}
}
private int age;
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string company;
public String Company
{
get
{
return company;
}
set
{
company = value;
}
}
private int salary;
public int Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}


 

#region IDataErrorInfo Members
 
public string Error
{
get { throw new NotImplementedException(); }
}
 
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "FirstName")
{

if (string.IsNullOrEmpty(FirstName))
result = "Please enter a Name";
//if (!Regex.IsMatch(FirstName, "^[a-zA-Z ]+$"))
//{
// result = "Name doesnot contain Numerical value";
//}
}

if (columnName == "Age")
{
if (Age <= 0 || Age >= 99)
result = "Please enter a valid age";
}
if (columnName == "Company")
{
if (string.IsNullOrEmpty(Company))
result = "Please enter a Company Name";
}
if (columnName == "Salary")
{
if (Salary <= 0 )
result = "Not valid";
}
return result;
}
}
 
#endregion
Anmerin at 27-Dec-12 5:57am
   
am getting an exception while following the method which using regex.
Oleksandr Kulchytskyi at 27-Dec-12 6:03am
   
Please provide me with exception cotent
Oleksandr Kulchytskyi at 27-Dec-12 6:19am
   
Not exactly what i'm expectiong for..
You said : "am getting an exception while following the method which using regex."
Where Exception???
Anmerin at 27-Dec-12 6:25am
   
Sorry..
the exception which arises is
 
ArgumentNullException was unhandled by the user.
Value cannot be null.
Parameter name: input
Oleksandr Kulchytskyi at 27-Dec-12 6:41am
   
Yep, of course it must occur,
when you declare you class:
public class Customer : IDataErrorInfo you didn't provide any initialization of your class , so as a result if you check string.IsNullOrEmpty(FirstName), it it turn, it will return true!
And last one you must also inherit in INotifyPropertyChanged interface in your entities..., without such notification WPF wont update/display values properly...
 
Ohh yess. one more in for error notification (display func you must add appropriate style), so for textbox it must be:
 
Anmerin at 27-Dec-12 6:53am
   
I think its not need to inherit INotifyProperty here..
Am getting the validation result without using it.
if the Name field is empty ,am getting the error message which i given..
but now my task is to check whether the user input for the "name" field is of alphabets only or is der any numerical field ,en error message should be shown.i need the logic for this validation.
acutally am getting that execption whn iam codding the following for checking the name field
if (!Regex.IsMatch(FirstName, "^[a-zA-Z ]+$"))
{
result = "Name doesnot contain Numerical value";
}
Oleksandr Kulchytskyi at 27-Dec-12 7:07am
   
As concerns INPC , i think you are completelly mistake!
What would be if you develop some input , and while try to input FirstName , now do you think does WPF will track property changes ???
My answer is now...
Oleksandr Kulchytskyi at 27-Dec-12 7:05am
   
So, make double check! What a problem?)
if(string.IsNullOrEmpty(FirstName))
return "Name cannot be empty";
if (!Regex.IsMatch(FirstName, "^[a-zA-Z ]+$"))
result = "Name doesnot contain Numerical value";

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100