Create folder named Classes
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
2...
public class ViewBase: Window
{
#region " CloseCommandHandler Evnt Handler "
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
#endregion
}
3. Now create model folder and inside it create class
public class Customer : System.ComponentModel.INotifyPropertyChanged, IDataErrorInfo
{
#region " Data Member "
private string strFirstName;
private string strLastName;
private string strEmailAddress;
private string strContactNumber;
#endregion
#region " UserID Property "
/// <summary>
///
/// </summary>
public string UserID { get; set; }
#endregion
#region " FirstName Property "
/// <summary>
///
/// </summary>
public string FirstName
{
get { return strFirstName; }
set
{
strFirstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
#endregion
#region " FullName Property "
/// <summary>
///
/// </summary>
public string FullName { get { return string.Format("Customer Full Name: {0}, {1}", LastName, FirstName); } }
#endregion
#region " LastName Property "
/// <summary>
///
/// </summary>
public string LastName
{
get { return strLastName; }
set
{
strLastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
#endregion
#region " EmailID Property "
/// <summary>
///
/// </summary>
public string EmailID
{
get { return strEmailAddress; }
set
{
strEmailAddress = value;
RaisePropertyChanged("EmailID");
}
}
#endregion
#region " ContactNumber Property "
/// <summary>
///
/// </summary>
public string ContactNumber
{
get { return strContactNumber; }
set
{
strContactNumber = value;
RaisePropertyChanged("ContactNumber");
}
}
#endregion
#region " DateOfBirth Property "
/// <summary>
///
/// </summary>
public DateTime DateOfBirth { get; set; }
#endregion
#region " INotifyPropertyChanged Members "
#region " PropertyChanged Event "
/// <summary>
///
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region " RaisePropertyChanged Function "
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
#endregion
#endregion
#region " IDataErrorInfo Members "
#region " Error Property "
/// <summary>
///
/// </summary>
public string Error
{
get { throw new NotImplementedException(); }
}
#endregion
#region " this Property "
/// <summary>
///
/// </summary>
/// <param name="columnName"></param>
/// <returns></returns>
public string this[string columnName]
{
get
{
string strMessage = string.Empty;
ValidateUserInput(ref strMessage, columnName);
return strMessage;
}
}
#endregion
#region " ValidateUserInput Input "
/// <summary>
///
/// </summary>
/// <param name="pstrMessage"></param>
/// <param name="pstrColumnName"></param>
private void ValidateUserInput(ref string pstrMessage, string pstrColumnName)
{
switch (pstrColumnName)
{
case "UserID":
if (string.IsNullOrEmpty(UserID))
pstrMessage = "User ID is required.";
break;
case "FirstName":
if (string.IsNullOrEmpty(FirstName))
pstrMessage = "First name is required.";
else if (string.IsNullOrWhiteSpace(FirstName))
pstrMessage = "Spaces are not allowed in First name. only character are allowed";
else if (FirstName.Length <= 2)
pstrMessage = "First name lenght should be at least 2.";
break;
case "LastName":
if (string.IsNullOrEmpty(LastName))
pstrMessage = "Last name is required.";
else if (string.IsNullOrWhiteSpace(LastName))
pstrMessage = "Spaces are not allowed in Last name. only character are allowed";
break;
case "ContactNumber":
if (string.IsNullOrEmpty(ContactNumber))
pstrMessage = "Contact Number is required";
else if (Regex.IsMatch(ContactNumber, @"^\d+$") == false)
pstrMessage = "Only digits are allowed in Contact Number field.";
break;
case "EmailID":
if (string.IsNullOrEmpty(EmailID))
pstrMessage = "Email ID is required.";
else if (Regex.IsMatch(EmailID, @"^[A-Za-z0-9_\-\.]+@(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$") == false)
pstrMessage = "Please enter valid email ID.";
break;
}
}
#endregion
#endregion
}
4. Create Customerview model
public class CustomerViewModal
{
#region " ViewTitle Property "
/// <summary>
///
/// </summary>
public string ViewTitle { get; set; }
#endregion
#region " Customers Property "
/// <summary>
///
/// </summary>
public Customer Customers { get; set; }
#endregion
#region " Default Constructor "
/// <summary>
///
/// </summary>
public CustomerViewModal()
{
Customers = new Customer() { ContactNumber = "123456789", DateOfBirth = Convert.ToDateTime("08/08/1981"), EmailID = "myname@hotmail.com", FirstName = "Brett", LastName = "Lee", UserID = "000-ABCD-001" };
ViewTitle = "Customer Form";
}
#endregion
#region " SaveCommand RelayCommand Type "
/// <summary>
///
/// </summary>
RelayCommand saveCommand;
#endregion
#region " SaveCommand Property "
/// <summary>
///
/// </summary>
public ICommand SaveCommand
{
get
{
if (saveCommand == null)
saveCommand = new RelayCommand(CommandExecute, CanCommandExecute);
return saveCommand;
}
}
#endregion
#region " CommandExecute Function "
/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
private void CommandExecute(object parameter)
{
MessageBox.Show(Customers.FullName);
}
#endregion
#region " CanCommandExecute Function "
/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
private bool CanCommandExecute(object parameter)
{
// Code here
return true;
}
#endregion
}
5..
Now create main window
<classes:ViewBase
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:classes="clr-namespace:Input_Validation_In_MVVM.Classes"
xmlns:local="clr-namespace:Input_Validation_In_MVVM.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="Input_Validation_In_MVVM.MainWindow"
Title="MainWindow" Height="320" Width="410"
AllowsTransparency="True" WindowStyle="None"
Background="Transparent"
WindowStartupLocation="CenterScreen"
>
<classes:ViewBase.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"/>
</classes:ViewBase.CommandBindings>
<classes:ViewBase.DataContext >
<local:CustomerViewModal/>
</classes:ViewBase.DataContext>
<Grid Background="Transparent" >
<Grid.RowDefinitions >
<RowDefinition Height="30"/>
<RowDefinition />
<RowDefinition Height="40"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<GroupBox Grid.RowSpan="4" Header="Customer Input Form" Margin="2" FontSize="18.667" Height="310" />
<Grid DataContext="{Binding Customers}" Grid.Row="1">
<Grid.RowDefinitions >
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="75*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding FullName}" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,5,0"/>
<TextBlock Text="User ID :" Grid.Row="1" />
<TextBox Grid.Row="1" Grid.Column="1" Margin="2,1,10,1" Text="{Binding UserID, Mode=TwoWay, ValidatesOnDataErrors=True}" Height="23"/>
<TextBlock Text="Frist Name :" Grid.Row="2" />
<TextBox Grid.Row="2" Grid.Column="1" Margin="2,1,10,1" Text="{Binding FirstName, Mode=TwoWay, ValidatesOnDataErrors=True}" Height="23"/>
<TextBlock Text="Last Name :" Grid.Row="3" />
<TextBox Grid.Row="3" Grid.Column="1" Margin="2,1,10,1" Text="{Binding LastName, Mode=TwoWay, ValidatesOnDataErrors=True}" Height="23"/>
<TextBlock Text="Email Address :" Grid.Row="4" />
<TextBox Grid.Row="4" Grid.Column="1" Margin="2,1,10,1" Text="{Binding EmailID, Mode=TwoWay, ValidatesOnDataErrors=True}" Height="23"/>
<TextBlock Text="Contact No. :" Grid.Row="5" />
<TextBox Grid.Row="5" Grid.Column="1" Margin="2,1,10,1" Text="{Binding ContactNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" Height="23"/>
<TextBlock Text="Date Of Birth :" Grid.Row="6" />
<DatePicker Grid.Column="1" Grid.Row="6" Margin="2,1,10,1" Text="{Binding DateOfBirth, Mode=TwoWay}" Height="23"/>
</Grid>
<Button Content="Save" Grid.Row="2" Grid.Column="1" Width="75" Margin="0,3,90,3" HorizontalAlignment="Right" Command="{Binding SaveCommand}" BorderThickness="2" Height="28"/>
<Button Content="Close" Grid.Row="2" Grid.Column="1" Width="75" Margin="0,3,10,3" HorizontalAlignment="Right" Command="ApplicationCommands.Close" BorderThickness="2" Height="28"/>
</Grid>
</classes:ViewBase>
Now Done
No comments:
Post a Comment