Sep
7
2009

WPF: TextBox Error Notification Through Style



Comments available as RSS 2.0

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">
...

The rest is after the jump.

    <Setter.Value>
      <ControlTemplate TargetType="{x:Type TextBoxBase}">
        <Border Name="Border" CornerRadius="1" Padding="2"
                Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
          <Border Name="ErrorContainer">
              <ScrollViewer Margin="0" x:Name="PART_ContentHost" Background="Transparent"/>
          </Border>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
          </Trigger>
          <Trigger Property="Validation.HasError" Value="true">
              <Setter TargetName="PART_ContentHost" Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}" />
              <Setter TargetName="ErrorContainer" Property="Padding" Value="0,0,18,0" />
              <Setter TargetName="ErrorContainer" Property="Background">
                  <Setter.Value>
                      <ImageBrush ImageSource="pack://application:,,,/Resources/images/error_16.png" Stretch="None" AlignmentX="Right" AlignmentY="Center" />
                  </Setter.Value>
              </Setter>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Comments

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment

Powered by WP Hashcash