bonjour,
j'aimerai pouvoir donner des exception pour la validation de certains champs, mais de manière détaillée. je m'explique:
j'utilise silverlight 3 et le pattern mvvm. dans ma vue, j'ai ce champs:
<TextBox x:Name="numero" Text="{Binding PersonneChoisie.numero, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
le but est que quand ce champs est vide ou null, j'ai un data validation qui intervient. Dans mon viewModel, j'ai bien créé les accesseurs pour mon PersonneChoisie, et ça fonctionne. (PersonneChoisie est un type Personne qui a trois propriétés: numéro, nom et prénom).
dans mon model, cette classe est définie comme cela:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public partial class Personne : object, System.ComponentModel.INotifyPropertyChanged {
private string nomField;
private string numeroField;
private string prenomField;
[System.Runtime.Serialization.DataMemberAttribute()]
public string nom {
get {
return this.nomField;
}
set {
if ((object.ReferenceEquals(this.nomField, value) != true)) {
this.nomField = value;
this.RaisePropertyChanged("nom");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string numero {
get {
return this.numeroField;
}
set {
if ((object.ReferenceEquals(this.numeroField, value) != true)) {
this.numeroField = value;
this.RaisePropertyChanged("numero");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string prenom {
get {
return this.prenomField;
}
set {
if ((object.ReferenceEquals(this.prenomField, value) != true)) {
this.prenomField = value;
this.RaisePropertyChanged("prenom");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
} |
cette classe est généré dans ma couche model lorque je référence mon service wcf. Dans ce service wcf, j'utilise bien mon type Personne, que j'ai écrit comme ceci:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [DataContract]
public class Personne
{
[DataMember]
public string numero {get; set;}
[DataMember]
public string nom { get; set; }
[DataMember]
public string prenom { get; set; }
public Personne(string num, string nom, string prenom)
{
this.numero = num;
this.nom = nom;
this.prenom = prenom;
}
} |
je sais que si je modifie le code généré dans ma couche model (pour la propriété numéro) par:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [System.Runtime.Serialization.DataMemberAttribute()]
public string numero {
get {
return this.numeroField;
}
set {
if ((object.ReferenceEquals(this.numeroField, value) != true)) {
if (string.IsNullOrEmpty(value))
throw new ValidationException("numéro requis");
else
{
this.numeroField = value;
this.RaisePropertyChanged("numero");
}
}
}
} |
j'ai bien ce que je veux à l'affichage.
seulement voilà, si je met à jour ma référence de service, tout est supprimé, et je doit tout le temps me repalucher les ValidationException.
Ce que je cherche donc c'est de mettre des balises dans la création de ma classe Personne pour que quand la classe proxy est générée, ça soit le code écrit ci dessus...
j'ai essayé le [Required(MessageError="numero requis")] mais ça ne génère rien de différent..
Merci de m'aider.
Partager