Boris
Boris

Reputation: 10234

WPF: Setting TextBlock Height to 0 when there is no text

TextBlock always occupies some height even if it doesn't contain any text. The height of the TextBlock is determined by the font size if no text is present, except in case when it is explicitly set by the user, of course. Is there a way to make the TextBlock size equal (0,0) if there is no text present (or to make it collapsed)? Thanks.

Note: I have created a converter that set's the Visibility property of the TextBlock to Collapsed if there is no text, but I was wondering if the same or similar solution is possible without any converter or code-behind coding, i.e. to make it behave as explained only by using XAML.

Upvotes: 5

Views: 1808

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178650

<Style TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Text" Value="">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 14

Related Questions