View containing another relative view

2019-01-27 08:28发布

I want to build a view, which will contain an injected view into a region (both will have their own ViewModels).

The first view will contain some action controls (new, save, delete, load buttons) that will "execute" over the second view, which will contain some field controls (TextBoxes for user input).

The first view (and its viewmodel) cannot the real (final) type of the view that is into it, because this type can vary (e.g.: customer fields, products fields, user fields).

The question is: How can I reach this by using MVVM in a right way?

标签: wpf mvvm Prism
1条回答
戒情不戒烟
2楼-- · 2019-01-27 08:45

If I understand your question right, you have a View containing a generic object, and you want a different template based on what the generic object is.

If that's the case, use a ContentControl and DataTemplates

<ContentControl Content="{Binding SomeGenericObject}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:CustomerViewModel}">
            <local:CustomerView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ProductViewModel}">
            <local:ProductView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:OrderViewModel}">
            <local:OrderView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

As for handling the generic CRUD operations in your ViewModel, see my answer to your other question about using a generic interface.

查看更多
登录 后发表回答