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

Posted by firoso on Stack Overflow See other posts from Stack Overflow or by firoso
Published on 2010-04-18T05:10:12Z Indexed on 2010/04/18 5:13 UTC
Read the original article Hit count: 792

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?

© Stack Overflow or respective owner

Related posts about mvvm-light

Related posts about mvvm