The required-field check is written, but the TextBox never turns red. Sometimes the check never runs at all. Sometimes it runs, Validation.GetHasError is true, and the screen still shows nothing.
This is a short summary. The full article, with measurements for each case on .NET 10, is here:
Why WPF Validation Errors Are Not Displayed, and Choosing Between IDataErrorInfo and INotifyDataErrorInfo
Start from Validation.GetHasError
-
false: no rule is attached, or the input has not reached the source yet.IDataErrorInfodoes nothing until the binding hasValidatesOnDataErrors="True", because that setting isfalseby default.INotifyDataErrorInfois the only approach that is on by default. Typed text is checked only when it reaches the source, which forTextBox.Texthappens on focus loss. -
true, but nothing on screen: the error template has nowhere to draw. A replacedWindowtemplate without anAdornerDecoratorleaves no adorner layer. Inside aScrollViewerthe layer still exists, so only some fields go silent. -
A red border with no message: this is just how the default
ErrorTemplateworks. It never shows the text.
The fix for the message
Supply your own template:
<ControlTemplate x:Key="FieldErrorTemplate">
<StackPanel>
<Border BorderBrush="#D13438" BorderThickness="1">
<AdornedElementPlaceholder x:Name="Adorned" />
</Border>
<TextBlock Foreground="#D13438"
Text="{Binding ElementName=Adorned,
Path=AdornedElement.(Validation.Errors)/ErrorContent}" />
</StackPanel>
</ControlTemplate>
Adorners take no layout space, so reserve a bottom margin. For new code where the view model owns validation, the article recommends INotifyDataErrorInfo, and remember to raise ErrorsChanged when an error clears.
A Japanese version is also available.
Related Articles
- Fixing a RelayCommand Whose CanExecute Does Not Update the Button State in WPF
- Reading WPF Binding Errors and Diagnosing Them with the Output Window
- Switching Controls Between Display and Edit Modes in WPF DataGrid Cells
- Controlling When TextBox Input Reaches the Source with UpdateSourceTrigger in WPF
- Calling TextBox UpdateSource from the View in WPF: Implementation and Pitfalls
- Binding to the Parent DataContext from Inside a WPF DataTemplate
- How to Display Selectable, Copyable Read-Only Text in WPF
Top comments (0)