2012-07-04 3 views
0

Dans mon application Windows Phone, j'utilise un contrôle RichTextBox. J'ai un texte très long, donc le contrôle standard n'en affiche qu'une partie.Contrôle RichTextBox personnalisé

Sur Internet, j'ai trouvé cette solution: Creating-scrollable-Textblock-for-WP7 - cela peut m'aider. Il sépare les blocs et crée un TextBlock pour chacun des blocs de texte. Mais comment puis-je faire cela pour un RichTextBox? Est-il possible, parce que le RichTextBox dans mon cas contient beaucoup de blocs?

Répondre

3

Vous pouvez résoudre ce problème en ajoutant plusieurs contrôles RichTextBox à l'intérieur d'une pile ou d'un défilement. Vous devez calculer la taille de RichTextBox tout en ajoutant chaque bloc de texte. Lorsque jamais la taille semble dépasser 2048 pixels en hauteur/largeur, vous devez ajouter le texte dans le nouveau TextBlack riche.

Trouvez l'exemple de code ci-dessous pour TextBlock implémenté de la même manière.

Etape 1:

<pre> 
<ScrollViewer Margin="10,0,0,70">    
<StackPanel Grid.Row="4" Margin="0,-36,12,12" x:Name="textBlockStackPanel"> 

<TextBlock x:Name="StorytextBlock" Margin="0,0,12,12" MaxHeight="2048" TextWrapping="Wrap" FontSize="24" TextTrimming="WordEllipsis" FontFamily="Segoe WP" d:LayoutOverrides="Width" Foreground="#FF464646" /> 

</StackPanel>      
</ScrollViewer> 
</pre> 

Etape 2:

invoquer simplement la méthode ProcessTextLength() pendant le chargement de la page.

 
private void ProcessTextLength(string story) 
     { 
      string storytext = story.Replace("\n\n", "\n\n^"); 
      List storylist = storytext.Split('^').ToList(); 
      List finalstorylist = new List(); 
      string currenttext = ""; 
      foreach (var item in storylist) 
      { 
       currenttext = this.StorytextBlock.Text; 
       this.StorytextBlock.Text = this.StorytextBlock.Text + item; 
       if(this.StorytextBlock.ActualHeight > 2048) 
       { 
        finalstorylist.Add(currenttext); 
        this.StorytextBlock.Text = item; 
       } 
       if (storylist.IndexOf(item) == storylist.Count - 1) 
       { 
        finalstorylist.Add(this.StorytextBlock.Text); 
       } 
      } 
      this.StorytextBlock.Text = ""; 
      foreach (var finalitem in finalstorylist) 
      { 
       string text = finalitem; 
       if (text.StartsWith("\n\n")) 
        text = text.Substring(2); 
       if (text.EndsWith("\n\n")) 
        text = text.Remove(text.Length - 2); 
       this.textBlockStackPanel.Children.Add(new TextBlock 
                  { 
                   MaxHeight = 2048, 
                   TextWrapping = TextWrapping.Wrap, 
                   FontSize = 24, 
                   TextTrimming = TextTrimming.WordEllipsis, 
                   FontFamily = new FontFamily("Segoe WP"), 
                   Text = text, 
                   Foreground = new SolidColorBrush(Color.FromArgb(255,70,70,70))                
                  });     
      }  
     } 

Ceci résoudra votre problème. S'il vous plaît marquer comme réponse si cela vous aide vraiment.

Merci Kamal.

Questions connexes