Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBlock Wrapping Property Doesn't Work

Tags:

wpf

xaml

In the following XAML, I am trying to wrap the TextBlock which binds to "PortfolioCodes" and "CommentaryText" but it seems that "Wrapping" doesn't work for TextBlock. I tried every possible suggestion I could find on this web site but all in vain. Can someone please help.

    <Grid>
    <ListBox ItemsSource="{Binding Path=Summaries}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="5" BorderBrush="LightGray" BorderThickness="1" Padding="4" Margin="4">
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="15"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0">No Of Security</TextBlock>
                        <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding Path=PortfolioSecurityCount}"></TextBlock>

                        <TextBlock Grid.Column="0" Grid.Row="1">Portfolio Code(s)</TextBlock>
                        <Grid Grid.Column="2" Grid.Row="1" >
                            <TextBlock TextWrapping="Wrap" Text="{Binding Path=PortfolioCodes}"></TextBlock>
                        </Grid>

                        <TextBlock Grid.Column="0" Grid.Row="2">Commentary Text</TextBlock>
                        <Grid Grid.Column="2" Grid.Row="2" >
                            <TextBlock Grid.Column="2" Grid.Row="2" TextWrapping="Wrap"  Text="{Binding Path=CommentaryText}"></TextBlock>
                        </Grid>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Based on Guge response, I have changed xaml as below and now its working.

    <Grid x:Name="LayoutRoot">
    <ListBox x:Name="SummaryListBox" ItemsSource="{Binding Path=Summaries}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="5" BorderBrush="LightGray" BorderThickness="1" Padding="4" Margin="4">
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="15"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0">No Of Security</TextBlock>
                        <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding Path=PortfolioSecurityCount}"></TextBlock>

                        <TextBlock Grid.Column="0" Grid.Row="1">Portfolio Code(s)</TextBlock>
                        <TextBlock Grid.Column="2" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Path=PortfolioCodes}" ></TextBlock>

                        <TextBlock Grid.Column="0" Grid.Row="2">Commentary Text</TextBlock>
                        <TextBlock Grid.Column="2" Grid.Row="2" TextWrapping="Wrap"  Text="{Binding Path=CommentaryText}"></TextBlock>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
like image 757
TheITGuy Avatar asked Apr 05 '11 10:04

TheITGuy


2 Answers

Change the width of your third ColumnDefinition from "Auto" to "*", that way it only takes up whatever is left of your horizontal space.

To try to explain this: Screen area in WPF is distributed in a two pass algorithm. First each visual element asks each child how much space it needs, with an indication of how much is available. These children do the same for their children. Then each visual element tells each child how much they are actually going to get. These children, again, do the same for their children.

Your code failed to do what you wanted because the Grid in the DataTemplate told its third column children they could have all the horizontal space they wanted ("auto") in the first run. Those textboxes then thought that they wouldn't have to wrap. So they just reported back their desired width, and one line worth of height. In the second run the Grid found that "auto" turned out to be a little less than what those children wanted. And the Grid still only gave them one line worth of height, so wrapping was out of the question. The children then had no other option left to them but to truncate the text.

When the third column width is set to "*", the grid will tell the children in the that column exactly how many horizontal pixels are left after the first column got their "auto" and the second column got their 15. Now the textboxes can figure out that they may want to wrap, and they report back "Ok, Dad, I'll make do with those measly horizontal pixels, but at least give me what I want in verticals". There is no limit to the vertical space, so they get what they need to present all their glorious content.

like image 179
Guge Avatar answered Nov 07 '22 19:11

Guge


Try to give width to your TextBlock, by default TextBlock takes all available space and doesn't wrap text

like image 20
Arsen Mkrtchyan Avatar answered Nov 07 '22 18:11

Arsen Mkrtchyan