3

I'm trying to implement what should be simple textbox validation for a WPF application, but I'm having some problems.

I used this guide: http://www.codeproject.com/Tips/690130/Simple-Validation-in-WPF

My textbox in MainWindow.xaml:

     <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="23" 
             Margin="93,111,0,0" TextWrapping="Wrap" VerticalAlignment="Top" 
             Width="120" Style="{StaticResource textBoxInError}"
             Validation.ErrorTemplate="{StaticResource validationErrorTemplate}">
        <TextBox.Text>
            <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:NameValidator/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

My NameValidator Class in MainWindow.xaml.cs:

    public class NameValidator : ValidationRule 
    {
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
        if (value == null)
            return new ValidationResult(false, "value cannot be empty.");
        else
        {
            if (value.ToString().Length > 3)
                return new ValidationResult(false, "Name cannot be more than 3 characters long.");
        }
        return ValidationResult.ValidResult;
      }
  }

My Static Resources in App.xaml:

        <ControlTemplate x:Key="validationErrorTemplate">
            <DockPanel>
                <TextBlock Foreground="Red" DockPanel.Dock="Top">!</TextBlock>
                <AdornedElementPlaceholder x:Name="ErrorAdorner"></AdornedElementPlaceholder>
            </DockPanel>
        </ControlTemplate>
        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

I can run the application without any errors, but the validation is never triggered.

7
  • Which part isn't working? The trigger? The Validation Method? etc Commented Jun 10, 2015 at 16:58
  • Sorry my problem was poorly worded. I updated the question with a screencast. No validation is firing at all, and there are no errors when I start my application. Commented Jun 10, 2015 at 17:03
  • I'm looking at the code you provided, and perhaps I'm blind but I do not see any code that subscribes to any of the text box control events (lostfocus, textchanged, etc) How are you triggering the validation when you type in the textbox? Commented Jun 10, 2015 at 17:09
  • Hey bill, I've tried PropertyChanged and LostFocus, but neither are giving me any results. <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">. This is supposed to be in Textbox.Text in the binding tag, correct? Commented Jun 10, 2015 at 17:10
  • 1
    Are you sure "PropertyChanged" fires when you type text in the textbox? The sample tutorial you linked is using lostfocus. If you try that event instead, does it work? Commented Jun 10, 2015 at 17:11

1 Answer 1

6

Using what you posted, it works fine for me, it produces the red "!" above the textbox. However, I DID remember to set my DataContext, ie.

public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

Without this, it won't work.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Thanks. That did it, I'm just a n00b.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.