Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to render WPF controls on top of the wpf WebBrowser control?

Tags:

browser

wpf

xaml

I need an embedded WebBrowser control in my application, and am having problems displaying WPF content on top of it. The application will sometimes show popups for editing data or to display errors, and the WebBrowser is getting drawn on top of the popups because it is a WinForms control.

The alternative I looked at here uses a Popup control to put items on top of the WebBrowser control, however my problem with Popups is they stay open when you switch to another application, and they do not move with your application when the user resizes/moves the app.

Is there an alternative way I could do this? The embedded web content is aspx pages and so not static HTML.

like image 803
Rachel Avatar asked Oct 13 '10 18:10

Rachel


People also ask

Can WPF application run in browser?

WPF Browser applications (or XAML Browser applications, or XBAP) are a specific kind of application that are compiled into . xbap extensions and can be run in Internet Explorer.

How can I access a control in WPF from another class or window?

Access of the controls from within the same assembly is hence allowed. If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean.

Can WPF be targeted to Web Browser Yes or no?

WPF only runs on windows. You can make a type of wpf application called xbap which runs in a browser.


3 Answers

Embedded WebBrowsers suck, unfortunately. If you're displaying actual, real, dynamic web content in your WebBrowser, you have to go through the pain of linking another Window to your hosted WebBrowser's window, and handling moving/resizing/et al yourself. I haven't seen another way that works.

If you're displaying static content (or content from a source that you can control or influence), you might consider displaying, say, RTF docs in a DocumentViewer instead, which doesn't have the icky airspace issues of the WebBrowser control.

like image 138
FMM Avatar answered Oct 23 '22 23:10

FMM


One way you can get around airspace issues is by creating a new frameless window and positioning it on top the webbrowser control. The main problem with this is keeping it positioned properly when the main window get moved/resized/etc.

like image 39
mdm20 Avatar answered Oct 24 '22 00:10

mdm20


The workaround that you could do is by making the height of the web browser control to zero, when some other control comes in front of Web browser control.

FIX: The standard fix is you can set the height of web browser to zero when you trigger some other control over it depends upon your scenario. Below, there is a sample implementation.

In MainWindow.Xaml include the events.

Activated="Window_Activated"
Deactivated="Window_Deactivated"

In Xaml.cs handle the scenario by setting the height.

private void Window_Activated(object sender, EventArgs e)
{
    wb.Height = double.NaN;
}

private void Window_Deactivated(object sender, EventArgs e)
{
    wb.Height = 0;
}
like image 20
Darey Avatar answered Oct 23 '22 22:10

Darey