Howto enable validation errors to show on TextBox on Composite Control #6939
-
I have a Composite Control with a TextBlock and a TextBox. It's a TextBox with a TextBlock Header and some other stuff. How can I make that TextBox show validation errors? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 14 replies
-
Not sure what so special about your composite control. So, probably answering your question, you basically can do similarly with your own controls. You can wrap content of HeaderTextBox with DataValidationErrors and pass Errors array to its property. |
Beta Was this translation helpful? Give feedback.
-
Or if you question was "how I can actually validate control and pass an error", it should be similar to WPF. Use INotifyDataErrorInfo interface or some framework that simplified some operations. For example ReactiveUI https://www.reactiveui.net/docs/handbook/user-input-validation/ |
Beta Was this translation helpful? Give feedback.
-
I have this same problem. I want to group some controls into a user control - a TextBox and two buttons. I create a UserControl, layout the TextBox and buttons, and in the code-behind bind the TextBox to a Text property, then register a DirectProperty for that same Text property. Now I add the UserControl into my Window and bind the Text property of the UserControl to a property in my ViewModel, which is a ReactiveValidationObject. In the ViewModel add validation for this property. What is the most efficient way to pass any validation errors through to the inner TextBox in my UserControl? Do I have to implement INotifyDataErrorInfo? Is there some sub-class of UserControl that already implements this? I noticed a method "AddOwnerWithDataValidation" that creates a DirectProperty, is it useful for this? What does it do? |
Beta Was this translation helpful? Give feedback.
-
I got the same problem: If I use a single TextBox control in my "page" view, then the errors show up as red text. That is all good. I don't want to change the look. If I place the TextBox control in a composite user control that also have a label, then I see no red text. I can wrap it in a Why dear lord, oh why ? How does this data validation really work? How does - for example - the TextBox control latch on to INotifyDataErrorInfo of the DataContext ? And is placing the control inside a composite user control somehow messing with this? I've tried making it a templated control. Same result. The Label and the TextBox displays and works fine, setting the template with style (thanks @timunie for your guide), but the red validation text still don't show. What am I missing here? (I'm using v11 RC1 and ObservableValidator from CT MVVM) |
Beta Was this translation helpful? Give feedback.
-
I made it really simple. No errors er displayed. This is my "control":
|
Beta Was this translation helpful? Give feedback.
-
I also tried this. No errors are displayed.
|
Beta Was this translation helpful? Give feedback.
-
Many of the examples and advice given relates to v0.10. I wonder if something has changed. |
Beta Was this translation helpful? Give feedback.
-
I wound up figuring out how to make this work. The docs definitely appear to be missing some details. One aspect is that you have to make sure to actually pass through the validation errors to your custom/composite control similar to how Avalonia's own TextBox does. It looks like your control's ContentTextProperty is correctly set up so you just need to modify the condition in your version of the method to check for that property instead. The other is that your control's template has to specify a DataValidationErrors control in its XAML that actually binds to the controls errors like this example from the Material.Avalonia project. |
Beta Was this translation helpful? Give feedback.
-
It works! Thanks @oshelton To the those looking for help on the same issue, the code below is what ended up working for me:
Would have loved to have something simpler that did not require a templated control though. |
Beta Was this translation helpful? Give feedback.
-
Another related issue I'm having is the "double issue" that you've been reporting here: CommunityToolkit/WindowsCommunityToolkit#3763. I'm getting that right now. It's an odd story. First I had some simple code like this:
which worked ok and displayed the error message once, but it did not notify the issue via NotifyDataErrorInfo.ErrorsChanged - and INotifyDataErrorInfo.HasErrors was empty. Then I changed the attributes to
Now the NotifyDataErrorInfo came alive, and my event dependant code fired, but it also displayed error message twice. Any ideas why? |
Beta Was this translation helpful? Give feedback.
-
yea its old but maybe u like somthing like that |
Beta Was this translation helpful? Give feedback.
https://docs.avaloniaui.net/docs/next/guides/development-guides/data-validation#manage-validationplugins