Showing pieces of HTML on WPF Form
Add a WebBrowser control to your application:
<Window x:Class="WpfBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WebBrowser Height="311" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="503" />
</Grid>
</Window>
And then use the NavigateToString
of the WebBrowser
to load the page from your html string:
webBrowser1.NavigateToString("<html><head></head><body>First row<br>Second row</body></html>");
Eitan
Updated on June 13, 2022Comments
-
Eitan almost 2 years
I'd like to show pieces of HTML on my form. I've looked at a few examples online regarding using a webform or document layout, etc and nothing works for me.
We have a system on our website where a user can enter a notification using a Real Text Editor (CKEditor). The message is saved in the DB and a WPF application on all the computers in the office should display the message.
I would like to support HTML in my WPF program. The HTML isn't in a page, it's a piece of HTML in the db that needs to be displayed in its rendered form on the WPF application.
For example, if someone enters
<img src="http://www.google.com/google.jpg" />
it will show the image in the wpf application when I retrieve it from the database and show it on the form.What's the best way to do this?
P.S. I can't use a web browser because the Windows has "AllowTranparency" set to true.
-
Eitan about 12 yearsWhat's weird is I did that. Your example works but on my form the browser doesn't show anything, it's completely white but when I right click it I can view source and see the HTML you put above.
-
Răzvan Flavius Panda about 12 years@Eitan: Please give example of what code you used: xaml and code behind. I called
NavigateToString
in MainWindow constructor right afterInitializeComponent();
and it works. -
Eitan about 12 yearsYeah I figure out why it didn't work, I have an animation that fades the program in. In order to do this I need to set Allow Transparency to true. When I do the web browser it doesn't work when AllowTransparency = true
-
Palec over 7 yearsSadly, even in 2016, this old non-WPF-native component is still the easiest and most sane options for displaying HTML in WPF applications.