asunrey
asunrey

Reputation: 875

Bind a list of the same type viewModels to a tabcontrol in Caliburn.Micro

I'm having trouble binding a list of ViewModels as items to a tab control.

//ShellViewModel.cs
private BindableCollection<RecentUnitViewModel> RecentUnitModels { get; set; }
<!-- ShellView.xaml -->
<GroupBox FontSize="16" Margin="10" FontWeight="DemiBold" Grid.Row="3">
    <GroupBox.Header>Last Seen</GroupBox.Header>
    <TabControl ItemsSource="{Binding RecentUnitModels}" >
    </TabControl>
</GroupBox>

Unexpected result: Displays the type "RecentUnitViewModel" instead of the View.

Upvotes: 2

Views: 2356

Answers (2)

Dan Bryant
Dan Bryant

Reputation: 27495

The Caliburn binding for a TabControl is easiest if you have a Conductor that you bind by convention (set the DataContext to the Conductor instance and name the TabControl "Items".) The issue here is that a TabControl instantiates a single content presenter that is shared by all tabs, but you need to somehow tell Caliburn.Micro to find the View for the ViewModel when it changes the content. I found that this works nicely:

        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl cal:View.Model="{Binding}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>

I've made the assumption here that the ViewModels you're binding are true ViewModels (not UserControls) and that you have separate Views (UserControls) that Caliburn.Micro is binding for you.

Upvotes: 9

brunnerh
brunnerh

Reputation: 184441

You property needs to be public...

Upvotes: 1

Related Questions