October 19, 2006
01:05 PM

Previously I talked about my Proxy Element which allows binding ordinary CLR Properties. Another place this becomes extremely useful is when hosting Windows Forms Controls.

A recent post on microsoft.public.windows.developer.winfx.avalon questioned how to display HTML in a Frame Control. Unfortunately I believe you can only ask a Frame to display HTML at a given URI. The workarounds I posted were to use the Windows Forms WebBrowser control or implement an Asynchronous Pluggable Protocol (not a simple task, but made easier by this article on CodeProject).

I think the WebBrowser is the best solution, ultimately Frame is just hosting the same thing but with less flexibility. The problem with WebBrowser is that we can't simply bind the HTML from XAML. Until now:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:swf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
        xmlns:utils="clr-namespace:Utils.Avalon;assembly=Utils"
    >
    <Window.DataContext>
        <![CDATA[<b>Hello</b> <a href="about:blank">HTML World</a>!]]>
    </Window.DataContext>
    <Grid>
        <utils:Proxy In="{Binding}" Out="{Binding ElementName=browser, Path=DocumentText}" />
        <wfi:WindowsFormsHost >
            <swf:WebBrowser x:Name="browser" />
        </wfi:WindowsFormsHost>
    </Grid>
</Window>
© Douglas Stockwell 2007
Creative Commons License Unless otherwise specified all "source code" examples are available for use under the Creative Commons Attribution-Noncommercial 3.0 License. Please contact me if you would like more flexible licensing terms.
Messenger Presence