|
In this article I will discuss about RichTextBox in Windows Phone.

Let's write code:
Step 1: Create a Silverlight for Windows Phone project.
Step 2: Add a RichTextBox and two buttons inside ContentPanel of MainPage.Xaml. In RichTextBox I haved used different formatting of text. Remember RichTextBox is readonly.
<RichTextBox x:Name="rtb" Margin="20,-300,0,0"> <Paragraph FontSize="34"> A <Italic>RichTextBox</Italic> <Bold>demo from</Bold> initial <Underline>XAML</Underline> . </Paragraph> <Paragraph> Visit the <Hyperlink NavigateUri="http://msdn.com" TargetName="_blank">MSDN</Hyperlink> site </Paragraph> </RichTextBox>
<Button x:Name="GetRichTextBoxXAML" Margin="20,340,0,0" Height="80" Width="430" Click="GetRichTextBoxXAML_Click" Content="Get RichTextBox XAML" /> <Button x:Name="SetRichTextBoxXAML" Margin="20,480,0,0" Height="80" Width="430" Click="SetRichTextBoxXAML_Click" Content="Set RichTextBox XAML" />
Step 3: Add below OnNavigatedTo method in MainPage.Xaml.cs. In this method I placed code to see how to format code for RichTextBox in codebehind.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); Run run = new Run(); run.Text = "A";
Italic italic = new Italic(); italic.Inlines.Add(new Run() { Text = "RichTextBox" });
Bold bold = new Bold(); bold.Inlines.Add(new Run() { Text = "demo from" });
Underline underline = new Underline(); underline.Inlines.Add(new Run() { Text = "Codebehind" });
Image img = new Image(); img.Source = new BitmapImage(new Uri("Tile-Mix.png", UriKind.Relative)); img.Height = 150; img.Width = 150;
InlineUIContainer ui = new InlineUIContainer(); ui.Child = img;
Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(run); paragraph.Inlines.Add(italic); paragraph.Inlines.Add(bold); paragraph.Inlines.Add(underline); paragraph.Inlines.Add(new LineBreak()); paragraph.Inlines.Add(ui); rtb.Blocks.Add(paragraph); }
Step 4: Add event handler of GetRichTextBoxXAML_Click. In the XAML you will notice there won't be any element of image which we have added in OnNavigatedTo method. The XAML property doesn't support InlineUIContainer.
private void GetRichTextBoxXAML_Click(object sender, RoutedEventArgs e) { MessageBox.Show(rtb.Xaml); }
Step 5: Add event handler to SetRichTextBoxXAML_Click. I put exact XAML of MainPage.Xaml to set in RichTextBox via codebehind.
private void SetRichTextBoxXAML_Click(object sender, RoutedEventArgs e) { rtb.Xaml = "<Paragraph FontSize=\"34\">A<Italic>RichTextBox</Italic><Bold>demo from</Bold> initial<Underline>XAML</Underline> .</Paragraph><Paragraph>Visit the<Hyperlink NavigateUri=\"http://msdn.com\" TargetName=\"_blank\">MSDN</Hyperlink> site</Paragraph>"; }
Now when you press button Set RichTextBox XAML button you will get below exception.

Step 6: To fix the above exception we need to add Section element with NameSpace. Now the modified code of SetRichTextBoxXAML_Click would be.
private void SetRichTextBoxXAML_Click(object sender, RoutedEventArgs e) { rtb.Xaml = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph FontSize=\"34\">A<Italic>RichTextBox</Italic><Bold>demo from</Bold> initial<Underline>XAML</Underline> .</Paragraph><Paragraph>Visit the<Hyperlink NavigateUri=\"http://msdn.com\" TargetName=\"_blank\">MSDN</Hyperlink> site</Paragraph></Section>"; }

This ends the article of RichTextBox in Windows Phone.
|