Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 263
Boris Avatar asked Apr 16 '11 19:04

Boris


1 Answers

<Style TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Text" Value="">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Trigger>
    </Style.Triggers>
</Style>
like image 183
Kent Boogaart Avatar answered Oct 12 '22 22:10

Kent Boogaart