简体   繁体   中英

UWP - How to show different View (UserControl) in each PivotItem when Pivot bound to list of ViewModels?

I'm building a UWP app using the Template10 Minimal template. I have a list of ViewModels that share a common base class. They are bound to a Pivot as follows:

ItemsSource="{Binding EnabledModels}"

I've setup multiple data templates to map each ViewModel concrete type to the View (UserControl) created for that particular ViewModel as follows:

<Pivot.Resources>
    <DataTemplate x:Key="gettingStarted" x:DataType="vm:GettingStartedViewModel">
        <v:GettingStartedPart DataContext="{Binding}"></v:GettingStartedPart>
    </DataTemplate>
    <DataTemplate x:Key="packageSelection" x:DataType="vm:PackageSelectionViewModel">
        <v:PackageSelectionPart DataContext="{Binding}"></v:PackageSelectionPart>
    </DataTemplate>
</Pivot.Resources>

I've not been able to determine how to get the View to actually display. Currently it will only display the type name of the ViewModel. I'm sure I've messed up the bindings somehow.

My ultimate goal is to present a Pivot with a series of data collection screens that all share a common base ViewModel, but each screen has it's own data needs. I'd like to keep the screens as separate UserControl views and dedicated ViewModels to make them easier to maintain independently.

I've looked for other patterns for multi-screen data capture in UWP that don't require separate pages but haven't had any luck.

Thanks for any guidance you can provide!

I was able to get the DataTemplateSelector to work so the Pivot would display a PivotItem containing the appropriate View (UserControl) for each ViewModel in my list of EnabledModels . Thank you to commenters Will and AVK Naidu.

Good resource for this situation available here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM