title: WPF Validation of Textbox, what should the binding look like?
body: <p>I'm trying to validate a textbox in WPF. I found some examples on the internet and I've put together some things. But right now it just validates everything as if it was wrong. I was told it's my binding, as I'm not sure what to bind to what I've come here for some clarification :)</p>
  
  <p>Here's my validator:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>class TextRangeValidator : ValidationRule
      {
          private int _minimumLength = 0;
          private int _maximumLength = 0;
          private string _errorMessage;
  
          public int MinimumLength
          {
              get { return _minimumLength; }
              set { _minimumLength = value; }
          }
  
          public int MaximumLength
          {
              get { return _maximumLength; }
              set { _maximumLength = value; }
          }
  
          public string ErrorMessage
          {
              get { return _errorMessage; }
              set { _errorMessage = value; }
          }
  
          public override ValidationResult Validate(object value,
              CultureInfo cultureInfo)
          {
              ValidationResult result = new ValidationResult(true, null);
              string inputString = (string)value.ToString();
              if (inputString.Length &lt; this.MinimumLength ||
                  (
                   inputString.Length &gt; this.MaximumLength))
              {
                  result = new ValidationResult(false, this.ErrorMessage);
              }
              return result;
          }
      }
  </code></pre>
  
  <p>Here's my xaml code:</p>
  
  <pre><code>&lt;TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" &gt;
      &lt;TextBox.Text &gt;
          &lt;Binding Path="Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="true"&gt;
              &lt;Binding.ValidationRules  &gt;
                  &lt;validators:TextRangeValidator
                      ValidatesOnTargetUpdated="True"
                      MinimumLength="10"
                      MaximumLength="10"
                      ErrorMessage="CPR nummer ikke gyldigt" /&gt;
              &lt;/Binding.ValidationRules&gt;
          &lt;/Binding&gt;
      &lt;/TextBox.Text&gt;
  &lt;/TextBox&gt;
  </code></pre>
  
  <p>Right now my binding property is just named name, I've tried to make it bind to the CreateUserCPRTextbox.Text but it doesn't work. How does this work?</p>