0

I followed the answer in

Displaying html from string in WPF WebBrowser control

to bind to list of html content, so I'm able to display it in a WPF program I've built. I can't figure out how to get the ' and " to display properly.

Displaying HTML from string in a WebBrowser control doesn't properly display ' or "

Here is my XAML:

    <Grid Grid.Column="1" Grid.Row="2">
      <Border BorderThickness="2" BorderBrush="{DynamicResource TitleBrush}"  Margin="50,0">
       <WebBrowser utility:BrowserBehavior.Html="{Binding SelectedEmail.EmailContent}" 
             ScrollViewer.VerticalScrollBarVisibility="Hidden"/>
      </Border>
    </Grid>

Here is my code:

public class BrowserBehavior
  {
    public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
            "Html",
            typeof(string),
            typeof(BrowserBehavior),
            new FrameworkPropertyMetadata(OnHtmlChanged));

    [AttachedPropertyBrowsableForType(typeof(WebBrowser))]
    public static string GetHtml(WebBrowser d)
    {
      return (string)d.GetValue(HtmlProperty);
    }

    public static void SetHtml(WebBrowser d, string value)
    {
      d.SetValue(HtmlProperty, value);
    }

    static void OnHtmlChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
      WebBrowser webBrowser = dependencyObject as WebBrowser;
      if (webBrowser != null)
        webBrowser.NavigateToString(e.NewValue as string ?? "&nbsp;");
    }
  }

See the link above how with questions. Any help on how to get the ' or " to display would be much appreciate.

I have already tried to replace ' with &apos and &#39 with no results.

Example of Problem:

We have added several overlay options for the frame line. Each option will allow you to pick either “STD†(Doesn’t auto add butt doors) or “BUTT†(Auto adds butt doors).

Example of Proper Format:

We have added several overlay options for the frame line. Each option will allow you to pick either "STD" (Doesn't auto add butt doors) or "BUTT" (Auto adds butt doors).

Patrick95
  • 110
  • 12

1 Answers1

1

For that you can use a verbatim string and simply escape double quotes with an added double quote.

This sample works for me.

public partial class MainWindow : Window
{
    private string html = @"<!DOCTYPE html>
        <html lang=""en"">
            <body>
            <div>My Test HTML 'single quote', ""double quote""</div>
            </body>
        </html>";
    public MainWindow()
    {
        InitializeComponent();
        MyWebBrowser.NavigateToString(html);
    }
}

If you are reading the HTML from a file you shouldn't need to do anything special.

MyWebBrowser.NavigateToString(File.ReadAllText(@"C:\myfilepath\myfile.htm"));
Neil B
  • 2,096
  • 1
  • 12
  • 23