Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide <Run /> tag in TextBlock - WPF

Tags:

wpf

I have textblock with 2 Run tags and one Linebreak:

  <TextBlock>
      <Run Text="TopText"/>
      <LineBreak/>
      <Run x:Name="bottomRun" Text="Bottom text"/>
  </TextBlock>

I want to hide second Run tag in code behind. But there is no Visible property... Why is it so? What is the best solution how to hide only one Run tag?

like image 277
Alamakanambra Avatar asked Aug 11 '13 11:08

Alamakanambra


4 Answers

Visibility is the property in the UIElement class which all UI controls derive from but Run doesn't derive from it.

Best you can do is to set Text property to String.Empty in code behind:

bottomRun.Text = String.Empty;
like image 66
Rohit Vats Avatar answered Sep 22 '22 08:09

Rohit Vats


The TextBlock you've got is pretty small. When faced with a similar situation, I duplicated it and bound the Visiblity property on TextBlock.

<TextBlock Visibility="{Binding Path=LicenseValid, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false }">
      <Run Text="TopText"/>
      <LineBreak/>
      <Run x:Name="bottomRun" Text="Bottom text"/>
  </TextBlock>

<TextBlock Visibility="{Binding Path=LicenseValid, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false }">
      <Run Text="TopText"/>
      <LineBreak/>
      <Run x:Name="bottomRun" Text="Bottom text"/>
  </TextBlock>

The converter is suitably declared, defined and takes an 'invert' parameter.

like image 42
msr Avatar answered Sep 22 '22 08:09

msr


You can user Style Trigger with Binding :

<Run>
    <Run.Style>
        <Style TargetType="Run">
            <Setter Property="Text" Value="Bottom text"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=variable}" Value="{x:Null}">
                    <Setter Property="Text" Value=""/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Run.Style>
</Run>
like image 37
A. Morel Avatar answered Sep 18 '22 08:09

A. Morel


I know the OP wanted to solve this using a single TextBlock with Runs in it, but I solved the problem with a Horizontally oriented StackPanel of TextBlocks. It's a heavier solution since there are more controls involved, but works.

like image 45
Matt Becker Avatar answered Sep 22 '22 08:09

Matt Becker