How can I force the content load off all the TabItems of my TabControl in WPF? (using Caliburn.micro)

1k views Asked by At

I have a collection of Screens loaded into a tab control using Caliburn.Micro.

My problem is : Tab content is not loaded until I click on the tab to show it. I've already tried to change the selected index one by one, activate all items and etc but nothing works!

Does anyone know how could I solve this? I'm running out of ideas and hair. Thx!

<Controls:MetroWindow x:Class="App.Views.ShellView"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                  xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                  xmlns:cal="http://www.caliburnproject.org"
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  Dialog:DialogParticipation.Register="{Binding}"
                  BorderBrush="{DynamicResource AccentColorBrush}"
                  BorderThickness="1"
                  WindowStartupLocation="CenterScreen"
                  WindowState="Maximized"
                  mc:Ignorable="d">
<Grid>
    <TabControl Name="Items" >
    </TabControl>
</Grid>

public class ShellViewModel : Conductor<IScreen>.Collection.AllActive
{
    protected override void OnViewLoaded(object view)
    {
        base.OnViewLoaded(view);

        int x = 0;

        for(int x = 0; x < 5; x++)
        {
            MyTabViewModel myTabVm = new MyTabViewModel(x.ToString());
            Items.Add(myTabVm);
        }
    }
}

OnViewLoaded fires only when I click on the tab :(

public class MyTabViewModel : Screen
{
    public MyTabViewModel(string displayName)
    {
        this.DisplayName = displayName;
        this.ViewAttached += SistemaClaroViewModel_ViewAttached;
    }

    protected override void OnViewLoaded(object view)
    {
        GetBrowser().Navigate(new Uri("https://www.google.com.br/"));
        base.OnViewLoaded(view);
    }

    public WebBrowser GetBrowser()
    {
        return ((MyTabView)this.GetView()).Browser;
    }
}
2

There are 2 answers

0
Jonas Melo On BEST ANSWER

I've solved my problem by duplicating my views on my window and making them invisible. I think its an awful solution, but's all I've got. :/ If someone ends up here with any new suggestions, i'm all ears.

<!-- My visible tab control-->
<TabControl Name="Items"
                Grid.Row="0"
                SelectedValue="{Binding ActiveItem}" />

<!-- Duplicated, invisible, tab control's views (Only to force immediate loading -->
<ListView Grid.Row="1"
          ItemsSource="{Binding Items}"
          Visibility="Hidden">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ContentControl cal:View.Model="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
1
Jack Hughes On

One thing jumps out at me. Shouldn't the ShellViewModel be using the Conductor<IScreen>.Collection.OneActive base. Only one tab can be active at a time? You also need to bind the Activeitem onto the tab control. ActiveItem is a property of the OneActive base class.

Caliburn Micro have a sample utilising a tab control here.