Bonjour
J'ai une classe qui implemente INotifyDataErrorInfo
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
public class Contrainte : INotifyPropertyChanged, INotifyDataErrorInfo
    {
        public SortableObservableCollection<ElementGraphique>   _Liste;
        public SortableObservableCollection<ElementGraphique>   Liste 
        { 
            get { return _Liste; } 
            set { 
                _Liste = value; 
                _Liste.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Liste_CollectionChanged); 
            } 
        }
        public Int64                                            Id { get; set; }
        private String                  _Nom;
        //todo public  wcf_workflow.
 
        public String Nom
        {
            get { return _Nom; }
            set
                {
                if (IsDescriptionValid(value) && _Nom != value)
                {
                    _Nom = value;
                    onPropertyChanged(this, "Nom");
                }
            }
 
        }
        private String                  _Description;
        public String Description { get { return _Description; } set { _Description = value; onPropertyChanged(this, "Description"); } }
        public String SQL { get; set; }
        public List<wcf_workflow.clsDouble> ListeNomContrainte { get; set; }
 
        private Dictionary<String, List<String>> errors = new Dictionary<string, List<string>>();
        private const string DESC_ERROR = "Le nom n'est pas valide";
        public event PropertyChangedEventHandler PropertyChanged;
 
        private void onPropertyChanged(object sender, string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event System.Collections.Specialized.NotifyCollectionChangedEventHandler ListeChange;
        public Contrainte()
        {
            Liste = new SortableObservableCollection<ElementGraphique>();
 
        }
 
        void Liste_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
                foreach (var item in e.NewItems)
                    ((ElementGraphique)item).PropertyChanged += new PropertyChangedEventHandler(Contrainte_PropertyChanged);
            if (ListeChange != null)
                ListeChange(sender, e);
        }
 
        void Contrainte_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(sender, e);
        }
        internal static Contrainte FromWCF(wcf_workflow.Contrainte contrainte)
        {
            var res=new Contrainte();
            res.Id = contrainte.Id;
            res.Nom = contrainte.Nom;
            res.SQL = contrainte.SQL;
            res.Description = contrainte.Description;
            res.Liste = new SortableObservableCollection<ElementGraphique>();
            foreach (var item in contrainte.Liste)
            {
                res.Liste.Add(ElementGraphique.FromWCF(item));
            }
            foreach (var item in contrainte.Liste)
            {
                foreach (var prec in item.Precedents)
                {
                    res.Liste[contrainte.Liste.IndexOf(item)].Precedents.Add(res.Liste.FirstOrDefault(elem => elem.Id == prec.Id));
                }
            }
            return res;
        }
 
        internal static wcf_workflow.Contrainte ToWCF(Contrainte contrainte)
        {
            var res=new wcf_workflow.Contrainte();
            res.Id = contrainte.Id;
            res.Nom = contrainte.Nom;
            res.SQL = contrainte.SQL;
            res.Description = contrainte.Description;
            res.Liste = new List<wcf_workflow.ElementGraphique>();
            foreach (var item in contrainte.Liste)
            {
                res.Liste.Add(ElementGraphique.ToWCF(item));
            }
            foreach (var item in contrainte.Liste)
            {
                res.Liste[contrainte.Liste.IndexOf(item)].Precedents = new List<wcf_workflow.ElementGraphique>();
                foreach (var prec in item.Precedents)
                {
                    res.Liste[contrainte.Liste.IndexOf(item)].Precedents.Add(res.Liste[contrainte.Liste.IndexOf(prec)]);
                }
            }
            return res;
 
        }
 
 
 
        public bool HasErrors
        {
            get { return errors.Count > 0; }
        }
 
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
 
        public System.Collections.IEnumerable GetErrors(string propertyName)
        {
            return errors[propertyName];
        }
        public void AddError(string propertyName, string error, bool isWarning)
        {
            if (!errors.ContainsKey(propertyName))
                errors[propertyName] = new List<string>();
            if (!errors[propertyName].Contains(error))
            {
                if (isWarning)
                    errors[propertyName].Add(error);
                else
                    errors[propertyName].Insert(0, error);
                RaiseErrorsChanged(propertyName);
            }
        }
        public void RemoveError(string propertyName, string error)
        {
            if (errors.ContainsKey(propertyName) && errors[propertyName].Contains(error))
            {
                errors[propertyName].Remove(error);
                if (errors[propertyName].Count == 0)
                    errors.Remove(propertyName);
                RaiseErrorsChanged(propertyName);
            }
        }
        public void RaiseErrorsChanged(string propertyName)
        {
            if (ErrorsChanged != null)
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
        }
        public bool IsDescriptionValid(string value)
        {
            bool isValid = true;
            if (string.IsNullOrWhiteSpace(value))
            {
                AddError("Nom", DESC_ERROR, false);
                isValid = false;
            }
            else RemoveError("Nom", DESC_ERROR);
            if (ListeNomContrainte.Any(elem => elem.Texte.ToUpper().Equals(value.ToUpper())))
            {
 
                AddError("Nom", DESC_ERROR, false);
                isValid = false;
 
            }
            else RemoveError("Nom", DESC_ERROR);
            return isValid;
        }
 
 
 
 
    }
La propriete nom qui doit etre validée est bindé par une text box
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
<TextBox x:Name="TxtNomContrainte" Margin="344,8,519,9" TextWrapping="Wrap" Text="{Binding Path=Nom, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True,ValidatesOnNotifyDataErrors=True}" TextChanged="TxtNomContrainte_TextChanged" />
<sdk:ValidationSummary Grid.Row="3" Name="Erreur"/>
La validation n'a jamais lieu. quand je debug le code il se déroule correctement jusqu'au declenchement de l’évènement
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
if (ErrorsChanged != null)
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
où ErrorsChanged est null alors qu'il devrait être instancié par le binding.
Quelqu'un voit il une erreur ou a t'il déjà rencontré ce cas?