views:

3798

answers:

4

Hello,

I've been desperately looking for an easy way to display HTML in a WPF-application. There are some options:
1) use the WPF WebBrowser Control
2) use the Frame Control
3) use a third-party control

but, i've ran into the following problems: 1) the WPF WebBrowser Control is not real WPF (it is a Winforms control wrapped in WPF). I have found a way to create a wrapper for this and use DependencyProperties to navigate to HTML text with bindings and propertychanged. The problem with this is, if you put a Winforms control in WPF scrollviewer, it doesnt respect z-index, meaning the winform is always on top of other WPF control. This is very annoying, and I have tried to work around it by creating a WindowsFormsHost that hosts an ElemenHost etc.. but this completely breaks my binding obviously.

2) Frame Control has the same problems with displaying if it shows HTML content. Not an option.

3) I haven't found a native HTML-display for WPF. All options are winforms, and with the above mentioned problems.

the only way out I have at the moment is using Microsoft's buggy HtmlToXamlConverter, which crashes hard sometimes. (MSDN)

Does anybody have any other suggestions on how to display HTLM in WPF, without these issues?

sorry for the long question, hope anyone knows what i'm talking about...

+1  A: 

If you can't use WebBrowser, your best bet is to probably rewrite your HTML content into a FlowDocument (if you're using static HTML content).

Otherwise, as you mention, you kind of have to special-case WebBrowser, you're right that it doesn't act like a "real" WPF control. You should probably create a ViewModel object that you can bind to that represents the WebBrowser control where you can hide all of the ugly non-binding code in one place, then never open it again :)

Paul Betts
Well, now I'm indeed creating a FlowDocument using the HtmlToXamlConverter from Microsoft. But, since HTML is not always valid (see HTML generated by Exchange/Outlook), this function crashes sometimes. Because it is absolutely necessary to display something, I had to foresee a backup-scenario which displays the rough body, stripped of all tags and some tag replacements ("<br/>" -> "\n" etc...). This seems to work for now... thanks for your reply.
Roel
A: 

Another approach to working round the z-index limitation is to use a popup to overlay your WPF components over the HTML See http://karlshifflett.wordpress.com/2009/06/13/wpf-float-buttons-over-web-browser-control/ Note code below is taken straight from the link

<Grid>
  <WebBrowser x:Name="wbBrowser" />

  <Popup x:Name="puOverlay" AllowsTransparency="True" Placement="Bottom"
         PlacementTarget="{Binding ElementName=wbBrowser}">
    <Border x:Name="bdrOverLay" CornerRadius="30" BorderBrush="Blue"
            Background="#1F000000" Padding="7" BorderThickness="2">

      <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
          <Style TargetType="{x:Type Button}">
            <Setter Property="Width" Value="75" />
            <Setter Property="Margin" Value="3.5" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
          </Style>
        </StackPanel.Resources>
        <Button Command="NavigationCommands.BrowseBack" Content="Back" />
        <Button Command="NavigationCommands.BrowseForward" Content="Forward" />
        <Button Command="NavigationCommands.BrowseHome" Content="Home" />
        <Button Command="ApplicationCommands.Close" Content="Exit" />
      </StackPanel>

    </Border>
  </Popup>
</Grid>

Alternativly there is a 3rd party control that takes Win32 controls and renders them (as bit maps) into WPF http://www.codeplex.com/WPFWin32Renderer

David Waters
thanks for the information, but this is not a workable solution for me... too bad though.For now i'll stick with the html-to-xaml and parse routines.
Roel
A: 

have you tried the Awesomium? please refer to : http://chriscavanagh.wordpress.com/2009/08/25/a-real-wpf-webbrowser/

jing
A: 

This does not work. Microsoft sucks!

Pera Oeric