2012-04-12 45 views
0

我試圖驗證使用驗證在Silverlight的Silverlight 5驗證問題

這個我的項目的數據錄入結果

http://imageshack.us/photo/my-images/842/immagineleb.png/

你可以看到幾乎所有的邊界文本框是紅色的,實際上,在這種情況下,它們中沒有一個應該是紅色的!在所有的工具提示中都有相同的信息。

有我在形式

private int matricola; 
    public int Matricola 
    { 
     get { return matricola; } 
     set 
     { 
      ValidateRequiredInt("Matricola", value, "Inserire un numero"); 
      matricola = value; 
      OnNotifyPropertyChanged("Matricola"); 
     } 
    } 

    private String cognome; 
    public String Cognome 
    { 
     get { return cognome; } 
     set 
     { 
      ValidateRequiredString("Cognome", value, "Inserire cognome"); 
      cognome = value; 
      OnNotifyPropertyChanged("Cognome"); 
     } 
    } 

    private String nome; 
    public String Nome 
    { 
     get { return nome; } 
     set 
     { 
      ValidateRequiredString("Nome", value, "Inserire nome"); 
      nome = value; 
      OnNotifyPropertyChanged("Nome"); 
     } 
    } 

    private String codiceUtente; 
    public String CodiceUtente 
    { 
     get { return codiceUtente; } 
     set 
     { 
      ValidateRequiredString("CodiceUtente", value, "Inserire codice utente"); 
      codiceUtente = value; 
      OnNotifyPropertyChanged("CodiceUtente"); 
     } 
    } 

    private String password; 
    public String Password 
    { 
     get { return password; } 
     set 
     { 
      ValidateRequiredString("Password", value, "Inserire password"); 
      password = value; 
      OnNotifyPropertyChanged("Password"); 
     } 
    } 

    private int? idOrario; 
    public int? IdOrario 
    { 
     get { return idOrario; } 
     set { idOrario = value; } 
    } 

    private DateTime? dataAssunzione; 
    public DateTime? DataAssunzione 
    { 
     get { return dataAssunzione; } 
     set 
     { 
      if (value != null) 
      { 
       ValidateDateTime("DataAssunzione", (DateTime)value, "Immettere una data corretta"); 
       if (((DateTime)value).Year == 1 && ((DateTime)value).Month == 1 && ((DateTime)value).Day == 1) 
       { 
        dataAssunzione = null; 
       } 
       else 
       { 
        dataAssunzione = value; 
       } 
       OnNotifyPropertyChanged("DataAssunzione"); 
      } 
      else 
      { 
       dataAssunzione = null; 
      } 
     } 
    } 

    private DateTime? dataLicenziamento; 
    public DateTime? DataLicenziamento 
    { 
     get { return dataLicenziamento; } 
     set 
     { 
      if (value != null) 
      { 
       ValidateDateTime("DataLicenziamento", (DateTime)value, "Immettere una data corretta"); 
       if (((DateTime)value).Year == 1 && ((DateTime)value).Month == 1 && ((DateTime)value).Day == 1) 
       { 
        dataLicenziamento = null; 
       } 
       else 
       { 
        dataLicenziamento = value; 
       } 
       OnNotifyPropertyChanged("DataLicenziamento"); 
      } 
      else 
      { 
       dataLicenziamento = null; 
      } 
     } 
    } 

    private int idGruppoAnag; 
    public int IdGruppoAnag 
    { 
     get { return idGruppoAnag; } 
     set { idGruppoAnag = value; } 
    } 

    private int? costoOrario; 
    public int? CostoOrario 
    { 
     get { return costoOrario; } 
     set 
     { 
      if (value != null) 
      { 
       ValidateInt("CostoOrario", (int)value, "Immettere un numero o lasciare il campo vuoto"); 
       if (value == 0 || value == -1) 
       { 
        costoOrario = null; 
       } 
       else 
       { 
        costoOrario = value; 
       } 
       OnNotifyPropertyChanged("CostoOrario"); 
      } 
      else 
      { 
       costoOrario = null; 
      } 
     } 
    } 

的數據上下文中使用,並且這些是用於驗證

protected bool ValidateRequiredInt(string propName, int value, string errorText) 
    { 
     if (DataErrors.ContainsKey(propName)) 
     { 
      DataErrors[propName].Remove(errorText); 
     } 

     if (value == 0 || value == -1) 
     { 
      AddError(propName, errorText); 
      return false; 
     } 
     OnErrorsChanged(propName); 
     return true; 
    } 

    //validazione dei campi che richiedono numeri interi nullable 
    protected bool ValidateInt(string propName, int value, string errorText) 
    { 
     if (DataErrors.ContainsKey(propName)) 
     { 
      DataErrors[propName].Remove(errorText); 
     } 
     if (value == 0) 
     { 
      AddError(propName, errorText); 
      return false; 
     } 
     OnErrorsChanged(propName); 
     return true; 
    } 

    //validazione dei campi che richiedono stringhe 
    protected bool ValidateRequiredString(string propName, string value, string errorText) 
    { 
     //Clear any existing errors since we're revalidating now 
     if (DataErrors.ContainsKey(propName)) 
     { 
      DataErrors[propName].Remove(errorText); 
     } 
     if (string.IsNullOrEmpty(value)) 
     { 
      AddError(propName, errorText); 
      return false; 
     } 
     OnErrorsChanged(propName); 
     return true; 
    } 

    protected bool ValidateDateTime(string propName, DateTime value, string errorText) 
    { 
     //Clear any existing errors since we're revalidating now 
     if (DataErrors.ContainsKey(propName)) 
     { 
      DataErrors[propName].Remove(errorText); 
     } 
     if (value.Year == 9999 && value.Month == 12 && value.Day == 31) 
     { 
      AddError(propName, errorText); 
      return false; 
     } 
     OnErrorsChanged(propName); 
     return true; 
    } 

我還使用利用方法的對象的屬性一個數據轉換器,在兩個「數據」文本框和數字轉換器,在matricola和costo文本框中,作爲本地資源,我可以說它們工作正常。

我錯過了什麼?

回答

0

如何實現INotifyDataErrorInfo?

在一個視圖模型基類:

public abstract class BaseViewModel : INotifyPropertyChanged, INotifyDataErrorInfo 
{ 
    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

#endregion 

    #region INotifyDataErrorInfo Members 

    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; 

    public System.Collections.IEnumerable GetErrors(string propertyName) 
    { 
    .... 
    } 

    public System.Collections.IEnumerable GetErrors() 
    { 
    ... 
    } 

//Plus methods to push errors into an underlying error dictionary used by the above error get methods 
} 

展開這個,你將有可重複使用的基類的所有視圖模型。 在適當的設置器中驗證屬性。如果它們未通過驗證,則將錯誤推送到按屬性名稱鍵入的錯誤字典中。如果驗證成功,則從屬性的字典中刪除驗證錯誤(如果有的話)。

您需要時更改字典火ErrorsChanged事件,但是這可以通過具有保護

SetErrorForProperty(string propName, object error) 

方法whcih包裝這件事來實現。 清除錯誤可以通過傳遞null以這種方法來進行和/或通過具有單獨

ClearErrorsFroProperty(串PROPNAME)

方法。