Open SKSniperSK opened 1 year ago
There's a problem with this, however. You're going to run into issues with FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty
(docs)
This is commonly seen in WPF when binding a TextBox
to a double
(for example, see this Stack Overflow post)
Suppose:
TextBox
, where the user is supposed to enter an IPv4 address (e.g., 10.20.30.40
)TextBox
has an UpdateSourceTrigger
of PropertyChanged
FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty
The user types a 1
in the TextBox.
1
back to the source (your view model's Path
property)TrySetProperty
method conducts validation - it fails, because 1
is not a valid IP addressTrySetProperty
method does not propagate the change to the fieldTextBox
's display does not accept the 1
TextBox
remains emptyIn cases like these, your view model should represent the "working state", not the final "approved state".
Overview
The problem I would like to solve is: Don't change a value when it fails validation but instead invoke the ErrorsChanged event, all of this just with attributes / code generators.
More in detail: As soon as you don't want invalid values to be saved you can no longer work with simple attributes but have to use TrySetProperty. But TrySetProperty is missing an overload to invoke the ErrorsChanged event but only relies on the out parameter to deliver validation errors. Therefore any event based validation handling stops working and needs some kind of workaround (like firing additional OnPropertyChanged calls to process the validation errors or to manually call ValidateProperty again just so that the ErrorsChanged event gets invoked.
API breakdown
Usage example
which would generate something like
Breaking change?
I'm not sure
Alternatives
Currently I only see one workaround if event based validation handling is used, doing an additional, unneeded validation just to trigger the event:
Every other workaround ignores events and just processes the provided
IReadOnlyCollection<ValidationResult>
out variable.Additional context
No response
Help us help you
No, just wanted to propose this