這裏添加新客戶屏幕的一個簡單的例子,我驗證的名稱和Tel.No,該按鈕將不會啓用,直到allPropertiesValid屬性設置爲true,這意味着所有的驗證,通過
查看
<Window FlowDirection="RightToLeft" x:Class="GlassStore.AddNewSpecialCustomer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:GlassStore.ViewModels"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AddNewSpecialCustomer" Height="346" Width="463" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<local:AddNewSpecialCustomerViewModel/>
</Window.DataContext>
<Grid Background="{DynamicResource NormalBrush}">
<Button IsDefault="True" Command="{Binding Save}" Content="موافق" Height="39" HorizontalAlignment="Left" Margin="15,256,0,0" VerticalAlignment="Top" Width="76" />
<Label Content="إسم العميل" Height="36" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="79" />
<TextBox Text="{Binding specialCustomerName,Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" Height="36" HorizontalAlignment="Left" Margin="111,12,0,0" VerticalAlignment="Top" Width="209" />
<Label Content="المنطقة/المكان" Height="35" HorizontalAlignment="Left" Margin="12,67,0,0" VerticalAlignment="Top" Width="94" />
<TextBox Text="{Binding region}" Height="35" HorizontalAlignment="Left" Margin="111,67,0,0" VerticalAlignment="Top" Width="210" />
<TextBox Text="{Binding tel,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" Height="33" HorizontalAlignment="Left" Margin="111,119,0,0" VerticalAlignment="Top" Width="210" />
<Label Content="رقم الهاتف " Height="33" HorizontalAlignment="Left" Margin="12,119,0,0" VerticalAlignment="Top" Width="94" />
<Button IsCancel="True" Content="إلغاء" Height="39" HorizontalAlignment="Left" Margin="128,256,0,0" VerticalAlignment="Top" Width="78" />
<Label Content="{Binding errorMessage}" ToolTip="{Binding richMessage}" Foreground="{Binding messageColor}" Height="40" HorizontalAlignment="Left" Margin="12,182,0,0" VerticalAlignment="Top" Width="412" />
</Grid>
</Window>
視圖模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Media;
namespace GlassStore.ViewModels
{
class AddNewSpecialCustomerViewModel:ViewModelBase,IDataErrorInfo
{
private String _errorMessage;
private Brush _messageColor;
private String _richMessage;
private String _specialCustomerName;
private String _region;
private String _tel;
private bool _allPropertiesValid;
private Dictionary<string, bool> validProperties;
private Commands.RelayCommand _save;
public AddNewSpecialCustomerViewModel()
{
validProperties = new Dictionary<string, bool>();
validProperties.Add("specialCustomerName",false);
validProperties.Add("tel", false);
allPropertiesValid = false;
}
public String richMessage
{
get { return _richMessage; }
set
{
if (_richMessage != value)
{
_richMessage = value;
OnPropertyChanged("richMessage");
}
}
}
public String errorMessage
{
get { return _errorMessage; }
set
{
if (_errorMessage != value)
{
_errorMessage = value;
OnPropertyChanged("errorMessage");
}
}
}
public Brush messageColor
{
get
{
return _messageColor;
}
set
{
if (_messageColor != value)
{
_messageColor = value;
OnPropertyChanged("messageColor");
}
}
}
public bool allPropertiesValid
{
get { return _allPropertiesValid; }
set
{
if (_allPropertiesValid != value)
{
_allPropertiesValid = value;
OnPropertyChanged("allPropertiesValid");
}
}
}
public String specialCustomerName
{
get { return _specialCustomerName; }
set
{
if (_specialCustomerName != value)
{
_specialCustomerName = value;
OnPropertyChanged("specialCustomerName");
}
}
}
public ICommand Save
{
get
{
if (_save == null)
{
_save = new Commands.RelayCommand(param => CanAddSpecialCustomer(), param => AddSpecialCustomer());
}
return _save;
}
}
public String region
{
get { return _region; }
set
{
if (_region != value)
{
_region = value;
OnPropertyChanged("region");
}
}
}
public String tel
{
get { return _tel; }
set
{
if (_tel != value)
{
_tel = value;
OnPropertyChanged("tel");
}
}
}
private bool CanAddSpecialCustomer()
{
return allPropertiesValid;
}
private void AddSpecialCustomer()
{
try
{
GlassStoreBLL.SpecialCustomer spcustomer = new GlassStoreBLL.SpecialCustomer();
spcustomer.name = specialCustomerName;
spcustomer.region = region;
spcustomer.telNo = Int64.Parse(tel);
spcustomer.AddNewCustomer(spcustomer);
errorMessage = "نـجـاح";
messageColor = Brushes.Green;
region = "";
tel = "";
specialCustomerName = "";
}
catch (Exception d)
{
errorMessage = "خطأ أثناء تسجيل العميل الخاص";
richMessage = d.Message;
messageColor = Brushes.Red;
}
}
string IDataErrorInfo.Error
{
get { return null; }
}
string IDataErrorInfo.this[string columnName]
{
get
{
string error = String.Empty;
switch (columnName)
{
case "specialCustomerName":
error = validateSpecialCustomerName();
validProperties["specialCustomerName"] = String.IsNullOrEmpty(error) ? true : false;
validateProperties();
return error;
case "tel":
error = validateTel();
validProperties["tel"] = String.IsNullOrEmpty(error) ? true : false;
validateProperties();
return error;
default:
throw new ApplicationException("Wrong Property name being validated");
}
}
}
private string validateSpecialCustomerName()
{
if (String.IsNullOrEmpty(specialCustomerName))
{
return "يجب إدخال إسم العميل الخاص";
}
else
{
if (specialCustomerName.Length < 2) return "إسم العميل الخاص لا يمكن أن يكون حرفين";
}
return String.Empty;
}
private string validateTel()
{
int result;
if (int.TryParse(tel, out result))
{
if (tel.Count() < 6)
{
return "رقم التلفون لا يمكن أن يكون أقل من 6 أرقام";
}
else
{
if (result < 0)
{
return "لا يمكن إدخال قيمة سالبة هنا";
}
return String.Empty;
}
}
else
{
return "الرجاء إدخال أرقام فقط في رقم التلفون";
}
}
private void validateProperties()
{
foreach (bool isValid in validProperties.Values)
{
if (isValid == false)
{
allPropertiesValid = false;
return;
}
}
allPropertiesValid = true;
}
}
}