Eftekhari
Eftekhari

Reputation: 1021

Showing XAML formatted text in WPF TextBlock

This is the XAML version of a HTML string

    <Section xml:space="preserve" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
       <Paragraph>
           <Hyperlink NavigateUri="E6A88D2B.js" />
       </Paragraph>
       <Paragraph />
       <Paragraph>
           <Span Foreground="blue">
               <Run FontWeight="bold">NOW, the</Run>
           </Span>
           <Span>
               /ˌen əʊ ˈdʌb<Run FontStyle="italic">ə</Run>ljuː $ -oʊ-/ 
           </Span>
             <Run>BrE</Run>
             <Run /><Run />
             <Run>AmE</Run>
             <Run /><Run />
             <LineBreak />
           <Span>
             <Span FontWeight="bold">
               <Run Foreground="blue">(the National Organization for Women)</Run>
             </Span> 
            a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑
             <Run>Friedan</Run>
             , who also helped to start it
           </Span>
           <LineBreak />
         </Paragraph>
   </Section>

I converted HTML to XAML because TextBlock doesn't support html tags to show. And WebBrowser is not what I want

How could I show this XAML code in a TextBlock in formatted version?

Upvotes: 2

Views: 2784

Answers (1)

pushpraj
pushpraj

Reputation: 13679

TextBlock supports only Inlines so Sections and Paragraphs are the one which are not supported in the mentioned xaml

here is an example excluding the same

<TextBlock xml:space="preserve">
    <Hyperlink NavigateUri="E6A88D2B.js">click for E6A88D2B.js</Hyperlink>
    <LineBreak />
    <LineBreak />
       <Span Foreground="blue">
           <Run FontWeight="bold">NOW, the</Run>
       </Span>
       <Span>
           /ˌen əʊ ˈdʌb<Run FontStyle="italic">ə</Run>ljuː $ -oʊ-/ 
       </Span>
         <Run>BrE</Run>
         <Run /><Run />
         <Run>AmE</Run>
         <Run /><Run />
         <LineBreak />
       <Span>
         <Span FontWeight="bold">
           <Run Foreground="blue">(the National Organization for Women)</Run>
         </Span> 
        a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑
         <Run>Friedan</Run>
         , who also helped to start it
       </Span>
       <LineBreak />
</TextBlock>

I also added some text for hyperlink to make it visible

other options includes using a document viewer

example

<FlowDocumentScrollViewer>
    <FlowDocumentScrollViewer.Document>
        <FlowDocument>
            <Section xml:space="preserve"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
               <Paragraph>
                   <Hyperlink NavigateUri="E6A88D2B.js">click for E6A88D2B.js</Hyperlink>
               </Paragraph>
               <Paragraph />
               <Paragraph>
                   <Span Foreground="blue">
                       <Run FontWeight="bold">NOW, the</Run>
                   </Span>
                   <Span>
                       /ˌen əʊ ˈdʌb<Run FontStyle="italic">ə</Run>ljuː $ -oʊ-/ 
                   </Span>
                     <Run>BrE</Run>
                     <Run /><Run />
                     <Run>AmE</Run>
                     <Run /><Run />
                     <LineBreak />
                   <Span>
                     <Span FontWeight="bold">
                       <Run Foreground="blue">(the National Organization for Women)</Run>
                     </Span> 
                    a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑
                     <Run>Friedan</Run>
                     , who also helped to start it
                   </Span>
                   <LineBreak />
                 </Paragraph>
           </Section>
        </FlowDocument>
    </FlowDocumentScrollViewer.Document>
</FlowDocumentScrollViewer>

Upvotes: 3

Related Questions