Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can you get a XAML TextBlock to collapse when it contains no data?
    text
    copied!<p>I want to tell WPF: "<strong>If TextBlock contains no data, then don't show it.</strong>"</p> <p><strong>TRY #1</strong> with a <strong>simple trigger</strong> produces the error "<em>'Text' member is not valid because it does not have a qualifying type name.</em>":</p> <pre><code>&lt;StackPanel Margin="10"&gt; &lt;TextBlock Padding="10" Background="Yellow" Text="{Binding MainMessage}"&gt; &lt;TextBlock.Triggers&gt; &lt;Trigger Property="Text" Value="{x:Null}"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/Trigger&gt; &lt;/TextBlock.Triggers&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; </code></pre> <p><strong>TRY #2</strong> with a <strong>style trigger</strong> produces the error <em>The type 'style' does not contain a public type-converter class</em>:</p> <pre><code>&lt;UserControl x:Class="TestItemsSource234.Views.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;UserControl.Resources&gt; &lt;Style x:Key="MainMessageStyle" TargetType="TextBlock"&gt; &lt;Style.Triggers&gt; &lt;Trigger&gt; &lt;Trigger Property="Text" Value="{x:Null}"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/Trigger&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel Margin="10"&gt; &lt;TextBlock Style="MainMessageStyle" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p><strong>TRY #3</strong> with a <strong>style DataTrigger</strong> produces the same error <em>The type 'style' does not contain a public type-converter class</em>:</p> <pre><code>&lt;UserControl x:Class="TestItemsSource234.Views.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;UserControl.Resources&gt; &lt;Style x:Key="MainMessageStyle" TargetType="TextBlock"&gt; &lt;Style.Triggers&gt; &lt;Trigger&gt; &lt;DataTrigger Binding="{Binding MainMessage}" Value="{x:Null}"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/DataTrigger&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel Margin="10"&gt; &lt;TextBlock Style="MainMessageStyle" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p><strong>TRY #4</strong>: OK, that was a dumb oversight of mine, forgot the <strong>StaticResource</strong>, but even then both Try #2 and Try #3 get a new error <em>The type System.Windows.Trigger in Style is unknown</em>:</p> <pre><code>&lt;UserControl x:Class="TestItemsSource234.Views.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;UserControl.Resources&gt; &lt;Style x:Key="MainMessageStyle" TargetType="TextBlock"&gt; &lt;Style.Triggers&gt; &lt;Trigger&gt; &lt;Trigger Property="Text" Value=""&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/Trigger&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel Margin="10"&gt; &lt;TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p><strong>So how do I do this?</strong></p> <h1>ANSWER:</h1> <p>OK, so that was a maddening syntax hunt with a happy end, here's the version that works, hope it helps somebody, lessons learned:</p> <ul> <li>if trigger, then <strong>style</strong></li> <li>if style, then <strong>StaticResource</strong></li> <li>if binding, then <strong>DataTrigger</strong></li> </ul> <p>code that works:</p> <pre><code>&lt;UserControl x:Class="TestItemsSource234.Views.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;UserControl.Resources&gt; &lt;Style x:Key="MainMessageStyle" TargetType="TextBlock"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding MainMessage}" Value="{x:Null}"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel Margin="10"&gt; &lt;ItemsControl ItemsSource="{Binding DataTypeViews}"/&gt; &lt;TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&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