RichTextBox control is one of the most welcome controls in Beta release of Windows Phone 7.1 or Mango phone.

Beta version of RichTextBox control has come with many limitations  Sad smile

  • It is Read Only
  • Control does not appear in tool box
  • There is no default style.

There are two usual ways to add RichTextBox, one directly add on XAML as below,

 

image

Another in code as below,

clip_image004

If you just add RichTextBox in XAML and run application, surprisingly you will not get rendered RichTextBox on page Sad smile . This is due to absence of default style for RichTextBox.

So to work with RichTextBox we need to add Default Style for RichTextBox.  In below App.Xaml , I have added default style for RichTextBox.

App.Xaml


<Application
    x:Class="RichTextBox.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>

        <Style TargetType="RichTextBox">
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RichTextBox">
                        <Grid Background="Transparent">
                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="{StaticResource PhoneHorizontalMargin}">
                                <ContentControl x:Name="ContentElement" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

 

 

After adding style for RichTextBox, on running you will get RichTextBox on phone as below,

image

To add button in RichTextBox , we need to put Button control inside InlineUIContainer like below .

image

To add an Undeline text we need to put text in Underline  tag

 

image

To add Image in RichTextBox we need to put Image in InlineUIContainer

 

image

After adding RichTextBox with different paragraphs containing Button , Image , Bold Text and Underlined text XAML would be as below ,

MainPage.xaml


<phone:PhoneApplicationPage
    x:Class="RichTextBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <RichTextBox x:Name="rtxtbox" Margin="0,-84,0,0">
                <Paragraph>
                    <Bold Foreground="BLUE">Hey I am Blue and Bold !</Bold>
                </Paragraph>
                <Paragraph>
                    <InlineUIContainer>
                        <Image Source="Penguins.jpg" />
                    </InlineUIContainer>
                </Paragraph>
                <Paragraph >
                    Dhananjay Kumar is here on DebugMode
                </Paragraph>
                <Paragraph >
                    <InlineUIContainer>
                        <Button
                            x:Name="btnRch"
                            Content="RichTextBox Button "
                             Click="btnRch_Click" />
                    </InlineUIContainer>
                </Paragraph>
                <Paragraph>
                    <InlineUIContainer>
                        <Image Source="Penguins.jpg" Height="20" Width="30"/>
                    </InlineUIContainer>
                </Paragraph>
                <Paragraph>
                    <Underline>I am UnderLined</Underline>
                </Paragraph>

            </RichTextBox>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

 

 

 

When you run phone application, you should get output as below,

image

 

Note:

In XAML if you will notice there are two paragraphs one for Button and one for Image. But in output those Image and Button is not getting displayed. In next post I would discuss on the same. And I if you get solution of this problem before me please write to me about your investigation  Smile

Thanks  for reading .

Advertisements

5 thoughts on “RichTextBox in Windows Phone 7.1 or Mango

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s