Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wpf - View containing another relative view

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...