Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Error Styles only being rendered properly on visible tab of a tab control
    text
    copied!<p>I have a data object used to contain my UI data that supports <code>INotifyPropertyChanged</code> and <code>IDataErrorInfo</code>. Originally I had all of the UI controls displaying in one big WPF application and was happily seeing errors flagged via this custom style:</p> <pre><code> &lt;!-- Set error style for textboxes --&gt; &lt;Style x:Key="txtBoxErrStyle" TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="True"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="Validation.ErrorTemplate"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate&gt; &lt;DockPanel DockPanel.Dock="Right"&gt; &lt;AdornedElementPlaceholder /&gt; &lt;Image Source="Error.png" Height="16" Width="16" ToolTip="{Binding Path=AdornedElement.ToolTip, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Adorner}}}" /&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>I was reorganizing the program today and decided to distribute the various UI controls over several pages of a <code>TabControl</code>. The structure layout I am using for this is:</p> <pre><code>&lt;tabcontrol&gt; &lt;tabitem&gt; &lt;AdornerDecorator&gt; [.. various Stack Panels, Groups and UI controls moved from original layout ..] &lt;/AdornerDecorator&gt; &lt;/tabItem&gt; &lt;tabitem&gt; &lt;AdornerDecorator&gt; [.. various Stack Panels, Groups and UI controls moved from original layout ..] &lt;/AdornerDecorator&gt; &lt;/tabItem&gt; ... &lt;/tabcontrol&gt; </code></pre> <p>(I am using the <code>AdornerDecorator</code> as I had experienced in a previous program the error style not being re-rendered when swapping tab pages. I can't remember where I saw this but it did help me out.)</p> <p>Now when I start my program the error style correctly renders on the <code>TabItem</code> that is open when the program starts, but does not correctly render on the other (hidden) <code>TabItem</code>s. When I select (and reveal) one of those <code>TabItem</code>s the tool-tip of the error style is set, but the error icon image is not displayed.</p> <p>I also tested removing the custom style and revert back to the default WPF error style for textboxes and I still get a similar behaviour, i.e. no red box around the control on the <code>TabItem</code>s that are hidden when the program opens.</p> <p>So it seems that I am totally missing something that is stopping the error styles from correctly rendering on other than the open tab Item. Any ideas?</p> <p><strong>Edit Sep 3</strong> Changed description to support a better understanding of what I have seen</p> <p><strong>Talk about Déjà vu in 2014</strong></p> <p>It's November 2014 and today I had this stupid WPF problem with error templates not showing up on items presented in a tab controller. Something in the back of my mind suggests that I have seen this problem before. So I google, and the first thing that pops up is my own question from 2009!</p> <p>This time I see the comment from dkl which was added after I solved things the last time around. So I tried it his way and used this solution (which worked well and I didn't need to sprinkle an Adorner control over my tab controls):</p> <pre><code>&lt;Style x:Key="TextBoxErrorStyle" TargetType="TextBox"&gt; &lt;Style.Triggers&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="Validation.HasError" Value="True" /&gt; &lt;Condition Property="IsVisible" Value="True" /&gt; &lt;/MultiTrigger.Conditions&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="14pt" Margin="-15,0,0,0" FontWeight="Bold"&gt;* &lt;/TextBlock&gt; &lt;Border BorderBrush="Red" BorderThickness="2"&gt; &lt;AdornedElementPlaceholder Name="controlWithError"/&gt; &lt;/Border&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" /&gt; &lt;/MultiTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre>
 

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