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?
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;
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With