How to perform explicit Validation?
I've implemented a validation object and successfully bound it to an input control (textBox) in XAML such that when I modify the UI, the validation fires, and prevents the value from being updated.
I would like to trigger the validation explicitly, so that I can perform it as the result of a user click (eg. a Close button) and condition the closing of the window on successful validation.
I tried doing this in code.
Specify the UpdateSourceTrigger to "Explicit" in XAML in the Binding and validation declaration and then do the following in the click handler for my close button.
BindingExpression be = textBoxPosition.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
The question then is how to determine the result of the validation, I don't seem to have access to this info. from here. I could do the Validation manually through the Validation object's Validate method, but then I would have to retrieve the value from the edit control manually, which seems to defeat the purpose of using a binding and validation object in the first place.
2 Questions:
1) Is there a way I can specify in XAML that I want validation to be performed on the click of the Close button and to prevent the close if it fails?
2) How can I use the Binding and Validaion objects to perform validation before allowing the closing of the window (in code behind)?
you can do the following in code, but as you said we have to get the value and pass it manually
BindingOperations
.GetBinding to get the binding and use Validate method of the validationRule to check the validationresult
After calling the UpdateSource
you can do the following instead of the manual stuff(txt1 is a textbox)
txt1.GetValue(
Validation.ErrorsProperty); if the validation fails the count will be >1
Ahh, I see.
Anything wrong with this approach?
bool
HasError = (bool) textBoxPosition.GetValue(Validation.HasErrorProperty);Still wondering about whether this can be done in pure XAML. I would think that this validation pattern is common enough that there's built-in plumbing for it?
It would be sooooo sweet if our UI guys could just wire in the validation themselves in XAML, then I could just worry about encapsulating the business logic within validation objects.
Funny you should mention that. I'm currently developing a library to provide some much needed convenience controls and utility classes for WPF developers. The biggest pain I will be addressing is the lack of a simple grid control similar to the DataGridView in the land of Winforms. However, markup extensions for validation and the like are definitely on the agenda!
The project was JUST created on Codeplex August 1st and I will be uploading the first changeset to source control on Friday (consisting essentially of the first version of the Grid).
If you'd like to follow along and/or join the project you can find it here at http://www.codeplex.com/Wiki/View.aspx?ProjectName=wpftoolbelt Although, it won't be ready in time to help a project that is well under way, the goal is to be ready in time for RTM with a solid set of controls and utilities.
Michael
Yeah, isn't it possible to just call a single method to trigger every ValidationRule a page contains and make it verify if any one of them returns false? Actually, I forsee a lot of boilerplate code to do that job for the forms I am creating in XAML.
Also, I have made a ValidationRule to verify if there is a non-empty string in a TextBox. It works fine when the user enters the control, types text and then leave it. But if it is left blank or focused without typing anything, the ValidationRule doesn't trigger at all. Is there a way to change that behavior?
Thanks!
Marc Lacoursiere