2012-04-12 5 views
0

내가 실버실버 라이트 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; 
    } 

위해 사용되는 방법 인 객체의 속성이있다 두 개의 "데이터"텍스트 상자에 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)

방법을 갖는 본 방법에 의해 널 통과시킴으로써 수행 될 수있다.