Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...