locked
set xaml property via code for RichTextBlock control

    Question

  • Dear Team,

               i am using the Images and Text in "RichTextBlock".

    string xaml = @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <Paragraph> First <InlineUIContainer> <Image Source=""http://www.simbatech.in/Apti_1_PO/1.png"" Stretch=""None""/> </InlineUIContainer> second <InlineUIContainer> <Image Source=""http://www.simbatech.in/Apti_1_PO/2.png"" Stretch=""None""/> </InlineUIContainer> Third <InlineUIContainer> <Image Source=""http://www.simbatech.in/Apti_1_PO/3.png"" Stretch=""None""/> </InlineUIContainer> </Paragraph> </Section>"; RichTextBlock rtb = (RichTextBlock)richtextbox;

    //rtb.xaml=xaml;


    for "Richtextblock" there is no xaml property.how can i assign the value

    Thanks

     

    Saturday, October 11, 2014 11:01 AM

Answers

  • The RichTextBlock collection has a Block collection property to which you can add block such as Paragraphs. There is no Section class though.

    You could use the XamlReader.Load to create a Paragraph object from your string and then add this one to the BlockCollection of the RichTextBlock:

    string xaml =
                    @"<Paragraph xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                        First
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/1.png"" Stretch=""None""/>
                        </InlineUIContainer>
                       second
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/2.png"" Stretch=""None""/>
                        </InlineUIContainer>
                        Third
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/3.png"" Stretch=""None""/>
                        </InlineUIContainer>
                    </Paragraph>";
    
                Paragraph par = XamlReader.Load(xaml) as Paragraph;
    
         RichTextBlock rtb = (RichTextBlock)richtextbox;
                rtb.Blocks.Add(par);
    

    • Marked as answer by mann madhan1 Saturday, October 11, 2014 12:06 PM
    Saturday, October 11, 2014 11:58 AM

All replies

  • The RichTextBlock collection has a Block collection property to which you can add block such as Paragraphs. There is no Section class though.

    You could use the XamlReader.Load to create a Paragraph object from your string and then add this one to the BlockCollection of the RichTextBlock:

    string xaml =
                    @"<Paragraph xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                        First
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/1.png"" Stretch=""None""/>
                        </InlineUIContainer>
                       second
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/2.png"" Stretch=""None""/>
                        </InlineUIContainer>
                        Third
                        <InlineUIContainer>
                            <Image Source=""http://www.simbatech.in/Apti_1_PO/3.png"" Stretch=""None""/>
                        </InlineUIContainer>
                    </Paragraph>";
    
                Paragraph par = XamlReader.Load(xaml) as Paragraph;
    
         RichTextBlock rtb = (RichTextBlock)richtextbox;
                rtb.Blocks.Add(par);
    

    • Marked as answer by mann madhan1 Saturday, October 11, 2014 12:06 PM
    Saturday, October 11, 2014 11:58 AM
  • Thanks Magnus

    Saturday, October 11, 2014 12:07 PM