6

I have an html page that only contains <p>, <strong>, <br /> and <a> tags. I want to show this content in a XAML TextBlock in Windows 8. Is there any way to show that content in a TextBlock without losing the structure (e.g. paragraphs)? I don't want to use WebView because WebView can not be transparent.

ipman
  • 1,212
  • 2
  • 21
  • 29
  • 1
    Can you use a [RichTextBlock](http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.richtextblock)? You'd have to translate the HTML markup into paragraphs, etc. yourself. Except Metro doesn't support inline hyperlinks, though there is a [workaround](http://stackoverflow.com/a/9882353/292432). – arx Jul 15 '12 at 10:28
  • Yes, I can use a RichTextBlock. However, I do not know how to populate a RichTextBlock dynamically. I was using Binding. – ipman Jul 15 '12 at 10:35
  • 1
    [This answer](http://stackoverflow.com/questions/3762150/coloring-text-in-richtextbox-c-sharp/3762469#3762469) creates rich content dynamically. It's actually for Silverlight, but I'd be surprised if the code was much different. – arx Jul 15 '12 at 10:46

2 Answers2

10

I am developing a open source Windows 8 Metro RSS Reader app and I used HtmlUtilities.ConvertToText

You can see the source code implementation here http://metrorssreader.codeplex.com/SourceControl/changeset/view/17913#265003

Zubair Ahmed
  • 725
  • 8
  • 15
  • sweet zombie jesus thank you for linking to this. I had no idea this was baked into the frame work and lost a ton of time chasing my tail and getting nowhere! thank you! – SelAromDotNet Apr 11 '13 at 23:03
4

If you want to do it in XAMl, just add a converter.

    public sealed class TextToHtmlConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is string)
        {
            return HtmlUtilities.ConvertToText(value.ToString());
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Then in your XAML add a resource reference.

Then Bind with a converter:

Text="{Binding titleFull,Converter={StaticResource TextToHtmlConverter}}"

dotsa
  • 911
  • 1
  • 11
  • 15