WPF: TextBox Error Notification Through Style
Leave a Comment
Posted in Programming and WPF
By default, WPF doesn’t provide any visual indication when ValidationRules aren’t met for TextBoxes. When I was learning WPF, I read a post about providing error notification for TextBoxes through the user of a style. I can’t find the article/post, but it never worked properly for me so I’ve modified and fixed what I had. This only works if there are ValidationRules bound to the TextBox, and it only displays the first failed rule.
<style TargetType="{x:Type TextBoxBase}" x:Key="{x:Type TextBox}"> <setter Property="Margin" Value="2" /> <setter Property="SnapsToDevicePixels" Value="True"/> <setter Property="OverridesDefaultStyle" Value="false"/> <setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <setter Property="FocusVisualStyle" Value="{x:Null}"/> <setter Property="MinWidth" Value="80"/> <setter Property="MinHeight" Value="20"/> <setter Property="AllowDrop" Value="true"/> <setter Property="Background" Value="{StaticResource WindowBackgroundBrush}" /> <setter Property="BorderBrush" Value="{StaticResource SolidBorderBrush}" /> <setter Property="Template"> ... </setter></style>
The rest is after the jump.
