4
votes

I have a View with several DataTemplates. Each DataTemplate has a View and ViewModel like so:

<DataTemplate DataType="{x:Type viewModels:exampleViewModel}">
    <AdornerDecorator>
        <ScrollViewer>
            <views:exampleView />
        </ScrollViewer>
    </AdornerDecorator>
</DataTemplate>

Then I have a TabControl which is bound to a DataContext.

In the DataContext, there is a Collection which has a list of all the different viewModels referenced by the DataTemplates:

<TabControl
    DataContext="{Binding}"
    ItemsSource="{Binding Collection, Mode=OneWay}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

Obviously there is one 'main' ViewModel for the View that contains the TabControl.

This works pretty well, creating TabItems for me for each item in the Collection and also setting the content of each page.

I now want to move this over to Catel but have no idea on where to begin because (and correct me if I'm wrong) :

  1. I now should not have any reference to any other ViewModel within any ViewModel and

  2. Catel automatically will link up my Views and ViewModels for me.

Any suggestions?

1
Your current design is flawed... if your Collection property holds all of your view models, then that means that they are all instantiated and using up resources even when they are not being displayed. I'd use the move to Catel to rework your design if I were you.Sheridan
@Sheridan yup... that is why I am moving over to Catel? Any suggestions? On how to implement something similar in Catel, that is. The idea is obviously not to have any ViewModels instantiated, but rather use a collection of items from my modelpjdupreez

1 Answers

1
votes

I recommend moving to Catel. It does solve this issue (this is actually why it was written in the first place). For a good starting point, check out the extensive documentation:

https://catelproject.atlassian.net/wiki/display/CTL/Catel+documentation+Home

A good read is the getting started with WPF part:

https://catelproject.atlassian.net/wiki/display/CTL/Getting+started+with+WPF

Also check out the examples repository:

https://github.com/Catel/Catel.Examples