Wpf – How to force a TabItem to initialize content on load


[disclaimer: I am new to Visual Basic.]

In a WPF, I have a TabControl containing 2 TabItems:

The first TabItem contains a bunch of URLs.

The second TabItem consists of a DockPanel that contains a cefSharp webView (chromium embedded for .net)

When I click on a url in tab1 it loads a page in the browser contained in tab2… But, it only works if I have initialized the browser first by clicking on tab2.

After doing some searching, it looks like vb.net doesn't initialize the content in a TabItem until it becomes visible. (right?)

So, my question is, how can I force a non-selected tab to initialize its content on load, in the background? (ie. so I don't have to click on the tab or switch to it first)


As requested, here is the relevant code:

The relevant XAML consists of a single DockPanel named "mainBox"

<DockPanel Name="mainBox" Width="Auto" Height="Auto" Background="#afe0ff" />

And here is my "code behind" vb script:

Class MainWindow : Implements ILifeSpanHandler, IRequestHandler

    Shared web_view1 As CefSharp.Wpf.WebView
    Shared web_view2 As CefSharp.Wpf.WebView

    Public Sub init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Loaded

    'This is in a DockPanel created on the xaml named mainBox

        ' set up tabControl:
        Dim browserTabs As New TabControl()
        browserTabs.BorderThickness = Nothing

        Dim tab1 As New TabItem()
        tab1.Header = "My Tab"

        Dim tab2 As New TabItem()
        tab2.Header = "Browser"

        Dim tab1Content As New DockPanel()
        Dim tab2Content As New DockPanel()

        tab1.Content = tab1Content
        tab2.Content = tab2Content



        ' set up browsers:
        Dim settings As New CefSharp.Settings()
        settings.PackLoadingDisabled = True

        If CEF.Initialize(settings) Then

            web_view1 = New CefSharp.Wpf.WebView()
            web_view1.Name = "myTabPage"
            web_view1.Address = "http://stackoverflow.com/"

            web_view2 = New CefSharp.Wpf.WebView()
            web_view2.Name = "browserPage"
            web_view2.Address = "https://www.google.com"
            web_view2.LifeSpanHandler = Me
            web_view2.RequestHandler = Me

            AddHandler web_view2.PropertyChanged, AddressOf web2PropChanged


        End If

    End Sub
End Class

So, in its default state, tab1 is showing at start up — the browser on tab2 (web_view2) won't initialize until I click its tab or change to its tab via script. Hope this clears it up a bit.

Best Solution

Your code doesn't use Windows Forms' TabPage but perhaps this might still help

As we know, Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown. This is by design and you can call TabPage.Show() method one by one as a workaround.

via MSDN forums

Also, based on the above idea, have you tried the following.

tab2.isEnabled = True
tab1.isEnabled = True


tab2.Visibility = True
tab1.Visibility = True

Also, the BeginInit Method might help in your situation.

Related Question