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

 none
[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">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="FriendsModel:FriendData">
                        <Grid x:Name="ContentPresenterGrid"
                                  Background="Transparent"
                                  HorizontalAlignment="Stretch"
                                  Width="300"
                                  Margin="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Ellipse x:Name="AvatarImage" Grid.Column="0" Grid.Row="0" Margin="10" Width="40" Height="40">
                                <Ellipse.Fill>
                                    <ImageBrush ImageSource="{x:Bind AvatarSource,Mode=OneWay}"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <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="???" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Margin" Value="0"/>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>

    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

Answers

  • 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();
            }
        }

    Xaml:

    <Page.Resources>
            <local:VisabilityConverter x:Key="VisabilityConverter"/>
        </Page.Resources>
    
     <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,

    Roy


    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 MSDNFSF@microsoft.com.

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