As you can see from the title, I'm going to ask what was asked many times before. But still, after reading all these other questions, I can’t find a decent solution to my problem.
I have a model class with basic validation:
partial class Player : IDataErrorInfo { public bool CanSave { get; set; } public string this[string columnName] { get { string result = null; if (columnName == "Firstname") { if (String.IsNullOrWhiteSpace(Firstname)) { result = "Geef een voornaam in"; } } if (columnName == "Lastname") { if (String.IsNullOrWhiteSpace(Lastname)) { result = "Geef een familienaam in"; } } if (columnName == "Email") { try { MailAddress email = new MailAddress(Email); } catch (FormatException) { result = "Geef een geldig e-mailadres in"; } } if (columnName == "Birthdate") { if (Birthdate.Value.Date >= DateTime.Now.Date) { result = "Geef een geldige geboortedatum in"; } } CanSave = true;
This check is performed every time a property changes (therefore, every time a user types a character in a text field):
<TextBox Text="{Binding CurrentPlayer.Firstname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="137" IsEnabled="{Binding Editing}" Grid.Row="1"/>
This works great. Verification is in progress ( PropertyChanged code for the binding is executed in the VM on the CurrentPlayer property, which is the Player object).
Now I would like to disable the save button if the validation fails.
First of all, the easiest solutions seem to be found in this thread:
Enable Disable save button during validation using IDataErrorInfo
- If I want to follow the decision I have to write twice, because I can’t just use the index. Writing a double code is absolutely not what I want, so it is not a solution to my problem.
- The second answer on this topic sounded very promising, but the problem is that I have several fields that need to be confirmed. Thus, it all depends on the last property checked (therefore, if this field is filled correctly,
CanSave will be true, even though there are other fields that are still not valid).
Another solution I found is using the ErrorCount property. But since I check with every change in the property (and so on for every typed character), this is also impossible - how can I find out when to increase / decrease the ErrorCount value?
What would be the best way to solve this problem?
thanks
Bv202 source share