Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.errortemplate.aspx" rel="nofollow noreferrer">Validation.ErrorTemplate</a> property expects a <code>ControlTemplate</code> and no <code>UserControl</code>. That's why your code is not working. You should better keep your "ValidationErrorTemplate" and put it in a shared <code>ResourceDictionary</code> which you can access everywhere in your application (or where you need it). For example, you could put it in your <strong>App.xaml</strong> and then reference it using a <code>DynamicResource</code>.</p> <hr> <p><strong>EDIT:</strong> OK, here we go with a more detailed explanation. Unless it is a requirement, I think you would better work with XAML instead of too much code loading ResourceDictionaries and assigning properties. You can easily achieve the desired behavior in XAML like this:</p> <p>Open the file called "App.xaml" in your WPF application project. Visual Studio/Blend will create that file automatically for you. This file contains a section <code>&lt;Application.Resources&gt;</code>. Add your ControlTemplate to this section:</p> <pre><code>&lt;Application.Resources&gt; &lt;ControlTemplate x:Key="ValidationErrorTemplate"&gt; &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Right"&gt; &lt;AdornedElementPlaceholder/&gt; &lt;Image Name="ValidizorImage" Stretch="None" Source="validizor.gif" ToolTip="{Binding [0].ErrorContent}" ToolTipService.InitialShowDelay="0" ToolTipService.ShowDuration="60000"/&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;/Application.Resources&gt; </code></pre> <p>Now, simply assign this error template to your controls using the <code>Validation.ErrorTemplate</code> property like this:</p> <pre><code>&lt;TextBox ... Validation.ErrorTemplate="{DynamicResource ValidationErrorTemplate}" /&gt; </code></pre> <p>Of course, you must have a Binding with ValidationRules defined for the respective properties on the control (e.g. the <code>Text</code> property of a TextBox). In a simple app, this code works for me: The image is displayed if the validation fails.</p> <p>I am not an expert in doing all this in code as I highly prefer XAML to define the UI. But as far as I can say, your code looks OK to me. Does the validation really fail? Check that by removing the Validation.ErrorTemplate. If your TextBox gets a red border, then it does fail - otherwise there might be an error in your validation logic?!</p> <p>HTH, gehho.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload