title: How to Implement Binding Validation for Binding Collection
body: <p>I am new to WPF and having problem with validation of bound collection.</p>
  
  <p>I have DataGrid that is bounded to collection of polymorphic objects that are grouped together.</p>
  
  <p>The DataGrid has two columns: </p>
  
  <ul>
  <li>Lable oneTime bounded to the Display Name</li>
  <li>TextBox TwoWay bounded to the value when the TextBox LostFocus </li>
  </ul>
  
  <p>Each line of the DataGrid is bounded to one object from the base class. Each derived class has its own validation method and I am trying to perform the appropriate validation method for the TextBox based on the instance of the derived class.</p>
  
  <p>I can bind the validation based on the ExceptionValidationRule but the performances are not so well and the application blinks since it’s having a lot of exceptions. I also know that I can do that throw the event of the TextBox LostFocus and getting the source object but I am trying to use WPF validation for bounded objects if it’s possible.</p>
  
  <p>I tried to inherit the ValidationRule in the base class and pass it but it’s enter with the base class to the function and not with the derived bounded item
  I tried to create separate classes that implement the ValidationRule and DependencyObject and I am trying to pass to it the source object like explained here: <a href="http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html" rel="nofollow">http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html</a></p>
  
  <p>But I still can’t make it to work. For the simplicity I created a DependencyObject that has TestString property and I tried to bind it to the Display Name (The bounded path of the Label). But the set of the TestString is never called. </p>
  
  <p>How can I do that? How can I call the appropriate validation method? Can I pass the bounded object in some way to the Valuator?</p>
  
  <p>Thanks for your help,
  Sarit</p>
  
  <p>The code I use:</p>
  
  <p>Implementation class of DependencyObject:</p>
  
  <pre><code>class ManifestPropertyDependency : DependencyObject
  {
  
      private ManifestProperty _manifestPropertyInstance;
      public ManifestProperty ManifestPropertyInstance
      {
          get { return (ManifestProperty)GetValue(ManifestPropertyInstanceProp); }
          set
          {
              SetValue(ManifestPropertyInstanceProp, value);
          }
      }
  
      private string testString;
      public string TestString {
          get { return (string)GetValue(TestStringProp); }
          set { SetValue(TestStringProp, value); }
      }
      public ManifestPropertyDependency()
      {
          testString = "";
          _manifestPropertyInstance = new ManifestProperty();
      }
      public static readonly DependencyProperty ManifestPropertyInstanceProp =
         DependencyProperty.Register("ManifestPropertyInstance", typeof(ManifestProperty),
         typeof(ManifestPropertyDependency), new UIPropertyMetadata(new ManifestProperty()));
  
      public static readonly DependencyProperty TestStringProp =
         DependencyProperty.Register("TestString", typeof(string),
         typeof(ManifestPropertyDependency), new UIPropertyMetadata(""));
  }
  </code></pre>
  
  <p>Implementation class of ValidationRule:</p>
  
  <pre><code>class ManifestPropertyValidator : ValidationRule
  {
      private ManifestProperty _manifest;
      private ManifestPropertyDependency _manifestPropertyDependency;
      public string Stam { get { return _manifestPropertyDependency.TestString; } set { _manifestPropertyDependency.TestString = value; } }
      public ManifestPropertyDependency ManifestPropertyDependency
      {
          get { return _manifestPropertyDependency; }
  
  
          set
          {
              _manifestPropertyDependency = value;
              Stam = value.TestString;
              _manifest = value.ManifestPropertyInstance;
          }
      }
  
  
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
          try
          {
              string errorMessage = "";
              if (ManifestPropertyDependency.ManifestPropertyInstance.ValidateString((string)value, ref errorMessage))
              {
                  return new ValidationResult(true, null);
              }
              return new ValidationResult(false, errorMessage);
          }
          catch (Exception e)
          {
              return new ValidationResult(false, "Illegal characters or " + e.Message);
          }
      }
  }
  </code></pre>
  
  <p>The Xaml of the validation:</p>
  
  <pre><code>&lt;TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}" 
                                   Style="{StaticResource textStyleTextBox}"&gt;
      &lt;TextBox.Text&gt;
          &lt;Binding Path="Value" Mode="TwoWay"  
                   UpdateSourceTrigger="LostFocus"&gt;
              &lt;Binding.ValidationRules&gt;
                  &lt;Classes:ManifestPropertyValidator&gt;
                      &lt;Classes:ManifestPropertyValidator.ManifestPropertyDependency&gt;
                          &lt;Classes:ManifestPropertyDependency TestString="{Binding Path=DisplayName}"/&gt;
                      &lt;/Classes:ManifestPropertyValidator.ManifestPropertyDependency&gt;
                  &lt;/Classes:ManifestPropertyValidator&gt; 
              &lt;/Binding.ValidationRules&gt;
          &lt;/Binding&gt;
      &lt;/TextBox.Text&gt;
  &lt;/TextBox&gt;
  </code></pre>