2

I have a ListBox with custom rows to show the name and age of people.

This is the code of my custom rows of my ListBox:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Background="Black" HorizontalAlignment="Left" Margin="0" Width="{Binding ElementName=DummyGridWidthItems, Path=ActualWidth}" Height="{Binding ElementName=DummyGridWidthItems, Path=ActualHeight}" ShowGridLines="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="0.3*"/>
            </Grid.ColumnDefinitions>
            <Viewbox StretchDirection="Both" Grid.Column="0" Stretch="Uniform" HorizontalAlignment="Left" VerticalAlignment="Center">
                <TextBlock Background="Aqua" Foreground="#5a5a5a" TextWrapping="Wrap" Margin="5" TextAlignment="Left" Text="{Binding Name}"/>
            </Viewbox>
            <TextBlock Grid.Column="1" Foreground="#5a5a5a" Text="12" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

When i run this code, my TextBlock inside my Viewbox displays the next image for short and long strings:

enter image description here

As you can see, the long string gets too small. So i decided to search on the web on how to wrap the string in multiple lines, and i found this answer. I set Width values to get a resized text, and if needed, a resized text with multiple lines, but the result was this:

enter image description here

Even thoug the long string got multiple lines, it got smaller... The only good thing was that the small string resized better.

As my final try, i change the Viewbox Stretch property to "Fill" but i got this:

enter image description here

After all of these mess, i have 3 questions:

1) My application is meant to be executed on different screens sizes, so i want all the strings to be resized so the user can have a very easy way to read them, is this Viewbox - TextBlock the right way to achieve this?

2) How can i make TextBlock inside a Viewbox to have all avilable space filled with multiple lines?

3) If the answer i found was guiding me in the right direction, how can i get the right Width for the TextBlock and Viewbox?

Any other information about this problem, i would be happy to read it. Thank you.

Community
  • 1
  • 1
Fernando Santiago
  • 2,128
  • 10
  • 44
  • 75

0 Answers0