Ternary
Ternary

Reputation: 2411

MVVM Light Multiple Data Contexts

I am new to MVMM Light toolkit (note, I'm using Light on .NET 3.5). So far I am really starting to like the pattern save for two challenges.

Upvotes: 0

Views: 631

Answers (1)

Rachel
Rachel

Reputation: 132548

You can use Templates or DataTemplates to create a reusable template defining how an object should look.

For example,

<DataTemplate DataType="{x:Type local:MovieTicket}">
    <!-- Write the XAML for your Movie Ticket -->
</DataTemplate>

You can give your DataTemplate an x:Key to reference it specifically in an ItemTemplate or ContentTemplate, or leave it out so it will be used anytime WPF tries to draw a MovieTicket object

<ItemsControl ItemsSource="{Binding MovieTickets}" />

<ContentControl Content="{Binding SelectedMovieTicket}" />

For your second question, I think this would be a bad idea for individual controls, although its a common practice for complete sections of the program.

When using MVVM, your ViewModels are your application, not your UI.

If your window should display a list of Movies for the user to edit, and allow the user to Save or Cancel their changes, then your ViewModel should contain an ObservableCollection<Movie> and contain an ICommand SaveCommand and ICommand CancelCommand

You really want your View to only reflect your ViewModels, and not have them pulling the data they need from all over the ViewModel hierarchy.

Based on your question, you may be interested in a blog article I have about Navigation with MVVM which uses code very similar to what you're asking about. It shows how to change the UI using implicit DataTemplates, and contains a complete code sample that you can download and look at to get a better idea of the MVVM architecture.

Upvotes: 1

Related Questions