title: How can I Set Validation.ErrorTemplate in Code behind?
body: <p>I've been looking in StackOverflow, and several questions are done. But they don't work.</p>
  
  <p>I have this <code>PasswordBox</code>:</p>
  
  <pre><code>&lt;PasswordBox Width="160"
        HorizontalAlignment="Left"
        Margin="10,3"
        Name="textBoxUserPass"
        local:PasswordBoxAssistant.BindPassword="True"
        local:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, UpdateSourceTrigger=Explicit, 
        ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
        Validation.ErrorTemplate="{x:Null}" /&gt;
  </code></pre>
  
  <p>I first set the <code>ValidationErrorTemplate="{x:Null}"</code> so first time I load the <code>PasswordBox</code> it doesn't try to validate the box. </p>
  
  <p>When I find a error, I try to change the <code>Validation.ErrorTemplate</code> so it shows a red border (as the <code>ErrorTemplate</code> I have defined) like this:</p>
  
  <pre><code>BindingExpression bePassword = textBoxUserPass.GetBindingExpression(PasswordBoxAssistant.BoundPassword);
  if (bePassword != null)
  {
      bePassword.UpdateSource();
      if (bePassword.HasError)
      {
          var validationError = new ValidationError(new ExceptionValidationRule(), bePassword);
          Validation.MarkInvalid(bePassword, validationError);
          validationError.ErrorContent = Cultures.Resources.MessageNoPassword;
          var b = Resources["validationTemplate"] as ControlTemplate;
          Validation.SetErrorTemplate(textBoxUserPass, b);
          }
      }
  }
  </code></pre>
  
  <p>Where I have defined my <strong>validationTemplate</strong> on a <code>ResourceDictionary.xaml</code> like this:</p>
  
  <pre><code>    &lt;Style x:Key="validationTemplate" TargetType="{x:Type TextBox}"&gt;
      &lt;Setter Property="Validation.ErrorTemplate"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate&gt;
                  &lt;DockPanel LastChildFill="True"&gt;
                      &lt;TextBlock DockPanel.Dock="Right" 
                      Foreground="Red"
                      FontSize="12pt"&gt;
                      *
                      &lt;/TextBlock&gt;
                      &lt;Border BorderBrush="Red" BorderThickness="1" CornerRadius="5"&gt;
                          &lt;AdornedElementPlaceholder /&gt;
                      &lt;/Border&gt;
                  &lt;/DockPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>A regular box, as you can see.</p>
  
  <p>My problem comes <strong>when trying to find this resource</strong> to change my <code>Validation.ErrorTemplate</code> in code behind. I put a breakpoint, and this line:</p>
  
  <pre><code>var b = FindResource("validationTemplate") as ControlTemplate;
  </code></pre>
  
  <p>says that <strong>b</strong> is null. How I can find <code>"validationTemplate"</code> and use it??</p>