1

So i have a TextBlock like below where i am displaying a name in the middle with the binding Name.

<TextBlock>
    <Run Text="Hello"/> <Run Text="{Binding Name}" /><Run Text=","/>
</TextBlock>

Is there any way to put a condition here to that when 'Name' is null or even better if the current DataContext object is null then i dont display anything at all?

1

1 Answer 1

5

You can update the style of your TextBlock by setting the Visibility to Hidden or Collapsed to hide it. Just add a DataTrigger for the case of an empty string like Value="" and a null value like Value="{x:Null} bound to the Name property:

<TextBlock.Style>
    <Style TargetType="TextBlock">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Name}" Value="">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.