2012-04-12 12 views
0

私はあなたがほとんどすべての国境を見ることができるように、これは結果のSilverlight 5検証の問題

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

あるシルバー

に検証を使用して私のプロジェクトのデータ入力を検証しようとしていますテキストボックスは赤色ですが、実際には、この場合、赤色で表示する必要はありません!そして、すべてのツールチップに同じメッセージがあります。

iがフォーム

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; 
    } 

ために使用される方法であるオブジェクトのプロパティが存在します2つの "データ"テキストボックスにdataconverter、そしてmatricolaとcostoのテキストボックスにnumberconverterがあり、地元のリソースとして、私は彼らがうまく動作すると言うことができます。

私に何か不足していますか?

答えて

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イベントを発生する必要がありますが、これはこれを包み込むwhcih保護

SetErrorForProperty(string propName, object error) 

方法を有することによって達成することができます。エラーをクリア および/または別

ClearErrorsFroProperty(ストリングたpropName)

方法を有することによって、このメソッドにnullを渡すことによって行うことができます。