2012-04-12 116 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)

方法。