Windows 8 Store App: Wrap Text Around Image

This article shows how to show text that can be wrapped around an image in XAML.

Figure A
                                       Figure A

In the preceding screen shot, we can see the news headline of the Microsoft Bing News app. Both of the news has an image and news title because the title cannot be accomodated in the first three rows due to its length, the reason being the Text has been wrapped with the images that is shown above.

If the text is not wrapped around the image, it still looks good but not the best.

Figure A
                                 Figure B

The problem is if the title of the news is too long then without wrapping there is a lot of white space left that cannot be used as shown in Figure C.

Figure B
                                          Figure C

To achive this in XAML we will use the "RichTextBlock" to show text and "RichTextBlockOverflow". The only purpose of RichTextBlockOverflow is to display text content that does not fit in the bounds of a RichTextBlock or another RichTextBlockOverflow element.

I used two Grids, a parent and a child Grid. The parent grid contains 2 rows and 1 column.

Parent Grid

Parent Grid
                                                      Figure D

  1. <Grid Margin="5" >  
  2.      <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition Width="330"/>  
  4.      </Grid.ColumnDefinitions>  
  5.      <Grid.RowDefinitions>  
  6.               <RowDefinition Height="auto"/>  
  7.               <RowDefinition Height="auto"/>  
  8.               <RowDefinition Height="auto"/>  
  9.       </Grid.RowDefinitions>  
  10. </Grid>  
child grid
                                                               Figure E

Child Grid

contain the Image
                                                         Figure F

The child Grid will contain 1 row and 2 columns. The first column will contain the image and the second column will contain the News Title.

News Title
                                                         Figure G
  1. <Grid Grid.Row="0" Grid.Column="0">  
  2.             <Grid.RowDefinitions>  
  3.                   <RowDefinition Height="80"/>  
  4.             </Grid.RowDefinitions>  
  5.             <Grid.ColumnDefinitions>  
  6.                   <ColumnDefinition Width="90"/>  
  7.                   <ColumnDefinition Width="240"/>  
  8.             </Grid.ColumnDefinitions>  
  9. </Grid>  
Now put the child Grid inside the first row of the parent grid.

child Grid inside
                                                   Figure H

forst row
                                                      Figure I

Use the following XAML to put the child grid inside the parent grid:
  1. <Grid Margin="5" >  
  2.      <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition Width="330"/>  
  4.      </Grid.ColumnDefinitions>  
  5.      <Grid.RowDefinitions>  
  6.               <RowDefinition Height="auto"/>  
  7.               <RowDefinition Height="auto"/>  
  8.               <RowDefinition Height="auto"/>  
  9.       </Grid.RowDefinitions>  
  10.       <Grid Grid.Row="0" Grid.Column="0">  
  11.             <Grid.RowDefinitions>  
  12.                   <RowDefinition Height="80"/>  
  13.             </Grid.RowDefinitions>  
  14.             <Grid.ColumnDefinitions>  
  15.                   <ColumnDefinition Width="90"/>  
  16.                   <ColumnDefinition Width="240"/>  
  17.             </Grid.ColumnDefinitions>  
  18.       </Grid>  
  19. </Grid>  
Now here is the complete XAML code to design the preceding News templete in which the text is wrapping around the image.
  1. <Grid Margin="5" >  
  2.      <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition Width="330"/>  
  4.      </Grid.ColumnDefinitions>  
  5.      <Grid.RowDefinitions>  
  6.               <RowDefinition Height="auto"/>  
  7.               <RowDefinition Height="auto"/>  
  8.               <RowDefinition Height="auto"/>  
  9.       </Grid.RowDefinitions>  
  10.       <Grid Grid.Row="0" Grid.Column="0">  
  11.             <Grid.RowDefinitions>  
  12.                   <RowDefinition Height="80"/>  
  13.             </Grid.RowDefinitions>  
  14.             <Grid.ColumnDefinitions>  
  15.                   <ColumnDefinition Width="90"/>  
  16.                   <ColumnDefinition Width="240"/>  
  17.             </Grid.ColumnDefinitions>  
  18.        <Image Source="{Binding ImageUrl}" Height="70" Width="70" Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Left"/>  
  19. <RichTextBlock Grid.Column="1" Grid.Row="0" IsTextSelectionEnabled="False" VerticalAlignment="Top" HorizontalAlignment="Left"                    OverflowContentTarget="{Binding ElementName=firstOverflowContainer}" FontFamily="Segoe UI Bold" FontSize="20" TextTrimming="WordEllipsis" TextWrapping="WrapWholeWords">  
  20.  <Paragraph>  
  21.    <Run Text="{Binding Title}"  ></Run>  
  22.  </Paragraph>  
  23. </RichTextBlock>  
  24.  </Grid>  
  25. <RichTextBlockOverflow x:Name="firstOverflowContainer" Grid.Column="0" Grid.Row="1" />  
  26. <StackPanel Orientation="Horizontal" Margin="-10,0,0,0" Grid.Column="0" Grid.Row="2">  
  27.   <TextBlock Text="{Binding PostedBy}"  FontSize="11"                   Margin="10,2,0,0" Width="auto" Height="42"                 TextTrimming="WordEllipsis" TextWrapping="Wrap"                        HorizontalAlignment="Left" FontWeight="Bold"/>  
  28. <TextBlock Text="{Binding CreatedDate}"  Margin="5,2,0,0"  FontSize="11" Width="auto" Height="42"     Foreground="Black"                       TextTrimming="WordEllipsis" HorizontalAlignment="Right" TextWrapping="Wrap" Opacity="0.49" />  
  29. </StackPanel>  
  30. </Grid>  
Thanks

Cheers!!

 


Similar Articles