Solving The Size Limitation Of Controls And Large Text Using TextBlock In Windows Phone

In this article we are going to see how to solve the windows controls size limitation.

There is a limitation on size of a control in Windows Phone of 2048 pixels only but in some case we need to display a long list of items. For example, TextBlock control is used to show large text but it’s truncated automatically if it more than 2048 pixels.

You can notice that if you place more text in textblock then it will disappear automatically due to 2048 pixel issue.

So my idea is to measure the string length if it goes more than the maximum length then add that into new textblock and bind to content presenter. Let us see the sets to perform this task.

Create new windows phone application and first we are going to see the XAML UI part.

The default control ContentPresenter is available in windows phone. Using this we can bind or add the other controls inside the content presenter.

So I am going to use this control, add content presenter inside the Grid as in the following, 

<ContentPresenter x:Name="cpresenter"></ContentPresenter>

Add this content presenter in the listview items template for scrolling the text block.

The full xaml source code looks like the following,

  1. <Grid>  
  2.     <ListBox x:Name="test" Margin="0,0,0,10" Background="#FF001918">  
  3.         <ListBox.ItemTemplate>  
  4.             <DataTemplate>  
  5.                 <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">  
  6.                     <ContentPresenter x:Name="cpresenter" Content="{Binding LargeText,Converter={StaticResource LargeTextConverter}}" Foreground="#FFEE1717"></ContentPresenter>  
  7.                 </StackPanel>  
  8.             </DataTemplate>  
  9.         </ListBox.ItemTemplate>  
  10.     </ListBox>  
  11. </Grid>  
Add new class and add one property to set the large text:
  1. publicclassvalues   
  2. {  
  3.     publicstring LargeText   
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8. }  
Next in code behind add new class file and make it as Converter like the following code.
  1. publicclassLargeTextConverter: IValueConverter   
  2. {  
  3.     publicobject Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)   
  4.     {  
  5.         UIElement result = null;  
  6.         // UIElement chat = null;  
  7.         if (value != null)   
  8.         {  
  9.             string data = value.ToString();  
  10.   
  11.             if (data.Length <= 500)   
  12.             {  
  13.   
  14.                 result = newTextBlock  
  15.                 {  
  16.                     Text = data,  
  17.                         TextWrapping = TextWrapping.Wrap,  
  18.                         Foreground = newSolidColorBrush(Colors.White),  
  19.                         FontSize = 15,  
  20.                         // Style = App.Current.Resources["ArticleBodyStyle"] as Style,  
  21.                         HorizontalAlignment = HorizontalAlignment.Left  
  22.                 };  
  23.             } else   
  24.             {  
  25.                 StackPanel resultPanel = newStackPanel();  
  26.                 int min = 0;  
  27.                 int max = 500;  
  28.                 while (true)   
  29.                 {  
  30.                     string temp = data.Substring(min, max - min);  
  31.                     if (max != data.Length)   
  32.                     {  
  33.                         int index = temp.LastIndexOf('\n');  
  34.                         index = index == -1 ? temp.LastIndexOf('.') + 1 : -1;  
  35.                         max = (index == -1) ? max : index;  
  36.                         temp = data.Substring(min, max) + '\n';  
  37.                         resultPanel.Children.Add(newTextBlock   
  38.                            {  
  39.                             Margin = newThickness(12, 0, 12, -30),  
  40.                                 Text = temp,  
  41.                                 TextWrapping = TextWrapping.Wrap,  
  42.                                 FontSize = 15,  
  43.                                 Foreground = newSolidColorBrush(Colors.White),  
  44.                                 // Style = App.Current.Resources["ArticleBodyStyle"] as Style,  
  45.                                 HorizontalAlignment = HorizontalAlignment.Left  
  46.                         });  
  47.                     } else   
  48.                     {  
  49.                         resultPanel.Children.Add(newTextBlock   
  50.                         {  
  51.                             Margin = newThickness(12, 0, 12, 0),  
  52.                                 Text = temp,  
  53.                                 TextWrapping = TextWrapping.Wrap,  
  54.                                 FontSize = 15,  
  55.                                 Foreground = newSolidColorBrush(Colors.White),  
  56.                                 // Style = App.Current.Resources["ArticleBodyStyle"] as Style,  
  57.                                 HorizontalAlignment = HorizontalAlignment.Left  
  58.                         });  
  59.                         break;  
  60.                     }  
  61.   
  62.                     min += max;  
  63.                     max = min + 500;  
  64.                     if (max > data.Length)  
  65.                     {  
  66.                         max = data.Length;  
  67.                     }  
  68.                 }  
  69.   
  70.                 result = resultPanel;  
  71.             }  
  72.         }  
  73.   
  74.         return result;  
  75.     }  
  76.   
  77.   
  78.     publicobject Convert(object value, Type targetType, object parameter, string language)   
  79.     {  
  80.         // throw new NotImplementedException();  
  81.         return Convert(value, targetType, parameter, newCultureInfo(language));  
  82.     }  
  83.   
  84.     publicobject ConvertBack(object value, Type targetType, object parameter, string language)   
  85.     {  
  86.         return Convert(value, targetType, parameter, newCultureInfo(language));  
  87.     }  
  88. }  
Here I checked the string length more than 500 characters then I add it to new textblock control and finally push this textblock control to Stack Panel. You can set the properties of the font also.

This solution is not perfect but anyway it will work for 2048 pixel problems and works fine for showing large text.

Now run the app and show the output like the following screen.

SCREEN