The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

[UWP][Xaml]Change TextBlock Visability depending on Text.lengh RRS feed

  • Question

  • Hello,

    I have the following code

           <ListView Name="ListView_FriendsList" Visibility="Visible" ItemsSource="{x:Bind FriendsList,Mode=OneWay}" IsItemClickEnabled="False" SelectionMode="None">
                    <DataTemplate x:DataType="FriendsModel:FriendData">
                        <Grid x:Name="ContentPresenterGrid"
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            <Ellipse x:Name="AvatarImage" Grid.Column="0" Grid.Row="0" Margin="10" Width="40" Height="40">
                                    <ImageBrush ImageSource="{x:Bind AvatarSource,Mode=OneWay}"/>
                            <StackPanel VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" Margin="14,10,0,10">
                                <TextBlock FontSize="15" Text="{x:Bind DisplayName,Mode=OneWay}"/>
                                <TextBlock FontSize="19" Text="{x:Bind ExtraInfo,Mode=OneWay}" Visibility="???" />
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Margin" Value="0"/>

    I want the visibility of the TextBlock that show the ExtraInfo to be changed depending on whatever there is extra info or not.

    any ideas how?

    thanks in advance

    Sunday, July 22, 2018 10:54 PM


  • Hi Lersubem,

    This could be done by using a binding converter.

    You could pass the string to the converter. In the Convert() method, decide return Visible or Collapsed as you want. 

    I made a simple demo that you could refer how to use it.

    The converter class:

      public class VisabilityConverter: IValueConverter
            public object Convert(object value, Type targetType, object parameter, string language)
                string str = (string)value;
                if (str.Length>10 )
                    return Visibility.Visible;
                return Visibility.Collapsed;
            public object ConvertBack(object value, Type targetType, object parameter, string language)
                throw new NotImplementedException();


            <local:VisabilityConverter x:Key="VisabilityConverter"/>
     <TextBlock Text="{Binding TestString}" Visibility="{Binding TestString, Converter={StaticResource VisabilityConverter},Mode=OneWay}" Grid.Row="1"/>

    If you add a string that length is less than 10, the textblock won't show.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    • Marked as answer by Lersubem Monday, July 23, 2018 12:44 PM
    Monday, July 23, 2018 7:03 AM