Tagged: PropertyGrid Validation
xilconicParticipantDecember 22, 2016 at 12:28 pmPost count: 2
When using the PropertyGrid exposing a double property and the user types in some non-numerical text like “xilconic”, the value is immediately reverted back to its original value.
This behavior is inconsistent when binding a WPF TextBox to a double property.In this usecase one would type in “xilconic”, the TextBox gets a red border and the typed text remains there.
I’m guessing that this is due to how the PropertyGrid handles exceptions that occur during entering data (I’m speculating that exception coming from converting string to double is handled in the same way as throwing an exception from the property setter). I like to configure the PropertyGrid in such a way, that I get consistent behavior as with the WPF TextBox.
Having the object bound to the PropertyGrid implement INotifyDataErrorInfo doesn’t give me the required behavior, due to the exception being caused from somewhere inside the PropertyGrid (again, probably converting a string to double).
The behavior that the PropertyGrid shows when using data implementing INotifyDataErrorInfo (Red border around the PropertyItem, error text as tooltip) would be appropriate solution.
I hope you can help me out with my problem.
Thanks in advance,
XilconicFawzi [Xceed]MemberDecember 23, 2016 at 7:29 pmPost count: 722
Currently, if you want to see a red border for an integer PropertyItem in the PropertyGrid, you need to set conditions. For example : a value smaller than 10. In this case, typing a value of 8 will result in a red border around the propertyItem and the tooltip will be visible when the mouse goes over the PropertyItem name.
But typing a non-numeric character in an IntegerUpDown is not valid and shouldn’t happen. We want the properties “Text”(of type string) and “Value” (of type integer) from the IntegerUpDown to remain in sync. If we accept that the “Text” property is “aa” while its “Value” property is 9, trying to get those values will result in non-controlled values, an unwanted behavior.
The method converting the typed text into an integer is “Int32.TryParse” which fails to convert “aa” into an integer. Since we can’t apply this value to the “Value” property(since its an integer) and we want the “Text” and “Value” properties to be in sync, we rollback to the last valid value => “Value” property is never changed and “Text” property goes back to the string of the “Value” property.
You can always define an IntegerUpDown for a specific property and add a InputValidationError callback. This callback will be called when a rollback happens to set the back the last valid value in the “Text” property.xilconicParticipantDecember 27, 2016 at 1:13 pmPost count: 2
If I understand you correctly, you propose that I would add an event handler to the IntegerUpDown. This was I have a hook to when the user would write in text for an integer property (likewise for double properties).
From this suggestion I see a couple of problems.
1. InputValidationErrorEventArgs API does not seem to provide me with a way to prevent the rollback from happening. To achieve consistent behavior with the default WPF behavior, I want to keep the text in the internal TextBox (I’m assuming that your implementation of IntegerUpDown is using this element) all the while the bound numerical property is unchanged. The PropertyItem would then be marked as invalid, showing a red border and having a tooltip that explains for example being unable to cast to a double.
2. I can’t seem to find an easy way to attach to an event handler that the InputValidationError event you specified. Given that I’m using PropertyGrid.AutoGenerateProperties (as it should handle different object types set on PropertyGrid.SelectedObject), I’ve noticed that PropertyGrid.PropertyDefinitions is empty, EditorDefinitions is also empty and that the particular PropertyItem from ProperyGrid.Properties has Editor property null. How would one be able to reach the IntegerUpDown instance to perform the suggested event subscription?
3. Assuming that IntegerUpDowns implementation doesn’t support my requirement, I can offcourse define my own ITypeEditor implementation that does. If there a way that I can configure PropertyGrid to use this editor as default editor instead of the default IntegerUpDown? Or do I really have to decorate all my numerical properties with EditorAttribute?
With kind regards,
- You must be logged in to reply to this topic.