Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 17-Dec-12 22:32pm
v3

1 solution

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):
C#
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/[^]
 
Share this answer
 
v2
Comments
Anmerin 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 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:

<stackpanel datacontext="{Binding CurrentPerson}" orientation="Vertical">
<textbox text="{Binding Id,Mode=TwoWay}">
....

<ListBox ItemsSource="{Binding PersonList}" DisplayMemberPath="Id"
SelectedItem="{Binding CurrentPerson,Mode=TwoWay}"/>
Anmerin 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 27-Dec-12 5:57am    
am getting an exception while following the method which using regex.
Oleksandr Kulchytskyi 27-Dec-12 6:03am    
Please provide me with exception cotent

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