Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Clear Triggered Control Template
    primarykey
    data
    text
    <p>I am trying to achieve the "hint text" functionality for a TextBox in WPF. I can set the default text fine, but the problem comes when I want the control to return its appearance to a normal TextBox. Here is the trigger I have so far:</p> <p><strong>Trigger A</strong></p> <pre><code>&lt;Trigger Property="Text" Value="{x:Static sys:String.Empty}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="TextBox"&gt; &lt;TextBox&gt; &lt;TextBox.Background&gt; &lt;VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="UniformToFill"&gt; &lt;VisualBrush.Visual&gt; &lt;Label Content="{TemplateBinding TextBox.Tag}" Background="White"/&gt; &lt;/VisualBrush.Visual&gt; &lt;/VisualBrush&gt; &lt;/TextBox.Background&gt; &lt;/TextBox&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Trigger&gt; </code></pre> <p>This sets the <code>Background</code> to a <code>VisualBrush</code> when the <code>Text</code> property is empty. What I need to do is clear this <code>ControlTemplate</code> when the user selects the <code>TextBox</code> to input text.</p> <p>Here is what I tried:</p> <p><strong>Trigger B</strong></p> <pre><code>&lt;Trigger Property="IsKeyboardFocused" Value="True"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="TextBox"&gt; &lt;TextBox/&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Trigger&gt; </code></pre> <p>These two don't work together. I tested each by changing the <code>Background</code> colors. If I comment out either one, they will each work. If both are uncommented, Trigger A works and B is never seen. How can I remove/overwrite Trigger A?</p> <p>I know that the functionality of these templates is supposed to clear when the trigger condition is no longer met, but for example, Trigger A's setting will not go away when I enter text into the <code>TextBox</code> like it should. Like the Text property is still <code>String.Empty</code> or something.</p> <p>So what am I missing?</p> <p><strong>EDIT:</strong></p> <p>Here is the whole style (there's not much more to it):</p> <pre><code>&lt;UserControl.Resources&gt; &lt;Style TargetType="TextBox" x:Key="FormsTextBox"&gt; &lt;Setter Property="Width" Value="45"/&gt; &lt;Setter Property="Margin" Value="3 2 3 2"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Text" Value="{x:Static sys:String.Empty}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="TextBox"&gt; &lt;TextBox&gt; &lt;TextBox.Background&gt; &lt;VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="UniformToFill"&gt; &lt;VisualBrush.Visual&gt; &lt;Label Content="{TemplateBinding TextBox.Tag}" Background="White" Width="45"/&gt; &lt;/VisualBrush.Visual&gt; &lt;/VisualBrush&gt; &lt;/TextBox.Background&gt; &lt;/TextBox&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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