Rafael Ventura
Rafael Ventura

Reputation: 314

WPF WebBrowser in TabItem not showing

I'm using WPF and need to populate a dynamically generated TabControl with a number of tabs.

I'm having trouble with the WebBrowser element, which seems to successfully navigate (I can see the mouse changes cursor as I hover through different elements) but the browser only displays white.

My code is below:

WebBrowser browser = new WebBrowser();
TabItem aTabItem = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };

(Form.FindName("MyTabControl") as TabControl).Items.Add(aTabItem);
(Form.FindName("MyTabControl") as TabControl).SelectedItem = aTabItem;

browser.NavigateToString("http://www.google.com");

So basically I create the TabItem, add the WebBrowser to it, add the TabItem to the TabControl which will hold all tabs created.

When I try this the WebBrowser isn't displayed. If I swap it for a Label with some text, the Label will happily show up.

Could you give me some pointers? Thanks

Upvotes: 1

Views: 948

Answers (2)

Rafael Ventura
Rafael Ventura

Reputation: 314

Found the issue. The window where I had the WebBrowser was set as AllowsTransparency="true". I should have known better...

Upvotes: 1

Abin
Abin

Reputation: 2956

This works for me,

        WebBrowser browser = new WebBrowser();
        TabItem TI = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };
        tabcontrol.Items.Add(TI);            
        browser.Source= new Uri("http://www.google.com"); 

I have used the Source of WebBrowser control. and i am creating the Uri.

NavigateToString(string text) works like below from MSDN

If the text parameter is null, WebBrowser navigates to a blank document ("about:blank").
If the text parameter is not in valid HTML format, it will be displayed as plain text.
After navigation, Source will be null.

Upvotes: 0

Related Questions