简体   繁体   English

UWP-当数据透视表绑定到ViewModels列表时,如何在每个PivotItem中显示不同的视图(UserControl)?

[英]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. 我正在使用Template10 Minimal模板构建UWP应用。 I have a list of ViewModels that share a common base class. 我有一个共享通用基类的ViewModels列表。 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: 我设置了多个数据模板,以将每种ViewModel具体类型映射到为该特定ViewModel创建的View(UserControl),如下所示:

<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. 当前,它将仅显示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. 我的最终目标是为Pivot提供一系列数据收集屏幕,这些屏幕都共享一个通用的基本ViewModel,但是每个屏幕都有自己的数据需求。 I'd like to keep the screens as separate UserControl views and dedicated ViewModels to make them easier to maintain independently. 我想将屏幕保留为单独的UserControl视图和专用的ViewModel,以使它们更易于独立维护。

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. 我在UWP中寻找了用于多屏数据捕获的其他模式,这些模式不需要单独的页面,但是没有运气。

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 . 我能够使DataTemplateSelector正常工作,因此Pivot将在我的EnabledModels列表中显示一个包含每个ViewModel合适的View(UserControl)的EnabledModels Thank you to commenters Will and AVK Naidu. 谢谢评论员Will和AVK Naidu。

Good resource for this situation available here 这里有针对这种情况的好资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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