title: Binding Validation.ErrorTemplate in UserControl
body: <p>I have a user control that has a textbox where its text property is bound to a dependency property called SelectedValue.  When the user enters text, that value is validated against another DP called ItemsSource to see if it in there.  If not, I throws an error.  Everything works - the TB in the UC has the default red box around it when there is an error.  </p>
  
  <p>But I would like the user to be able to specify a ControlTemplate in XAML when creating an instance of the UC.  So I thought I could create another DP of type ControlTemplate and they could bind to.  This seems to work but how do I actually implement that in XAML?  If it do something like:</p>
  
  <pre><code>Validation.ErrorTemplate="{Binding ValidationTemplate}"
  </code></pre>
  
  <p>it throws an error saying "'ErrorTemplate' property cannot be data-bound.".  Below are the relevant parts of the code:</p>
  
  <pre><code>&lt;Canvas DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"&gt;
      ....
      &lt;TextBox x:Name="ValueTextBox"
               TextWrapping="NoWrap" 
               GotFocus="_ValueTextBox_GotFocus"
               Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}"
               Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualHeight}"
         -----&gt;Validation.ErrorTemplate="{Binding ValidationTemplate}"&lt;-----
               &gt;
  
          &lt;TextBox.Resources&gt;
              &lt;CollectionViewSource x:Key="UniqueNamesList" Source="{Binding ItemsSource}" /&gt;
          &lt;/TextBox.Resources&gt;
  
          &lt;TextBox.Text&gt;
              &lt;Binding Path="SelectedValue" &gt;
                  &lt;Binding.ValidationRules&gt;
                      &lt;l:InListValidator ValidationStep="RawProposedValue" 
                                         IgnoreCase="True" 
                                         UniqueNames="{StaticResource UniqueNamesList}" /&gt;
                  &lt;/Binding.ValidationRules&gt;
              &lt;/Binding&gt;
          &lt;/TextBox.Text&gt;
      &lt;/TextBox&gt;
      ....
  &lt;/Canvas&gt;
  </code></pre>
  
  <p>and the DP itself:</p>
  
  <pre><code>public object ValidationTemplate
  {
      get { return (ControlTemplate)GetValue(ValidationTemplateProperty); }
      set { SetValue(ValidationTemplateProperty, value); }
  }
  public static readonly DependencyProperty ValidationTemplateProperty =
      DependencyProperty.Register("ValidationTemplate"
                                  , typeof(ControlTemplate)
                                  , typeof(AutoCompleteComboBox)
                                  , new FrameworkPropertyMetadata(new ControlTemplate()));
  </code></pre>
  
  <p>Thank for any help.</p>
  
  <p>Ernie</p>
  
  <hr>
  
  <p><b>Update:</b></p>
  
  <p>Thanks Guys. I actually tried both Adi and Nit's responses.  Both worked but Adi was closer to what I was looking for with not having to define a template local to the User Control.  Nit's actually runs even if I do not actually create the template and just add the binding but the designer gives an error.  I did have to tweak your code a bit Adi to set it on the TextBox itself:</p>
  
  <pre><code>public ControlTemplate ValidationTemplate
  {
      get { return (ControlTemplate)GetValue(ValidationTemplateProperty); }
      set { SetValue(ValidationTemplateProperty, value); }
  }
  public static readonly DependencyProperty ValidationTemplateProperty =
      DependencyProperty.Register("ValidationTemplate"
                                  , typeof(ControlTemplate)
                                  , typeof(AutoCompleteComboBox)
                                  , new FrameworkPropertyMetadata(new ControlTemplate(), OnValidationTemplateChanged));
  
  private static void OnValidationTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
      if (e.NewValue != null)
      {
          AutoCompleteComboBox control = (AutoCompleteComboBox)d;
          Validation.SetErrorTemplate(control.ValueTextBox, (ControlTemplate)e.NewValue);
      }
  }
  </code></pre>
  
  <p>Thanks!</p>