Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the standard convention for defining nested view:viewmodel mapping in MVVM Light

so in classic MVVM examples ive seen DataTemplate definitions are used to map up View Models to Views, what is the standard way to do this in MVVM Light framework, and where should the mappings be located? Following are examples of what I'm doing now and what I'm talking about, blendability is important to me!

Main Window:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="STS2Editor.MainWindow"
        Title="{Binding ApplicationTitle, Mode=OneWay}"
        DataContext="{Binding RootViewModel, Source={StaticResource Locator}}">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/ApplicationSkin.xaml" />
                <ResourceDictionary Source="Resources/ViewMappings.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ContentControl Content="{Binding ApplicationManagementViewModel}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid> 
</Window>

In the above code, my RootViewModel class has an instance of the class ApplicationManagementViewModel with the same property name:

public ApplicationManagementViewModel ApplicationManagementViewModel {get {...} set {...} }

I reference the ResourceDictionary "ViewMappings.xaml" to specify how my view model is represented as a view.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:STS2Editor.ViewModel">
    <DataTemplate DataType="{x:Type local:ApplicationManagementViewModel}">
        <local:ApplicationManagementView/>
    </DataTemplate>
</ResourceDictionary>

should I be doing things like this using ViewModelLocator? what about collections of view models?

like image 668
Firoso Avatar asked Oct 14 '22 06:10

Firoso


1 Answers

The method you use (with implicitly typed DataTemplates) works OK in WPF, but unfortunately it does not work in Silverlight. This is one of the reason why I prefer to use a more explicit method which works in both worlds.

Also, implicitly typed DataTemplates can be a bit confusing, because it is not always quite clear where the template comes from. That can render the work of the integrator very difficult at times, especially for small changes to the UI (been there, done that :)

There is no obligation to use the ViewModelLocator in MVVM Light, it is just a way that works well and is quite easy to understand (for people reading the code who are not familiar with the subtleties of WPF/SL). In the end, it is very much a matter of preference, but lately the ViewModelLocator pattern seems to gain in popularity (see for example this post where a generic ViewModelLocator is used together with MEF).

http://www.johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/

Finally, let me add that I am not very satisfied with the current implementation of the ViewModelLocator in MVVM Light, and I want to propose a much more generic solution in the next version.

like image 69
LBugnion Avatar answered Nov 10 '22 00:11

LBugnion