Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / WPF

MVVM for Multi Platforms

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
22 Mar 2010CPOL2 min read 26.3K   354   22  
How to implement MVVM when developing a view model whose view implementation language is not certain
<Grid x:Class="GeographicRepresentation.UILib.View.SelectedObjectView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:GeographicRepresentation.UILib.View"
    >    
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CommonViewDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <DataTemplate x:Key="readOnlyContinentDataTemplate">
                <DataTemplate.Resources>
                    <Style BasedOn="{StaticResource textBlockStyle}" TargetType="{x:Type TextBlock}" />
                </DataTemplate.Resources>
                <Grid VerticalAlignment="Top">                    
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Name" Grid.Column="0" Grid.Row="0" />
                    <TextBlock Text="{Binding Path=ContinentName}" Grid.Column="1" Grid.Row="0" />
                    <TextBlock Text="Number of countries" Grid.Column="0" Grid.Row="1" />
                    <TextBlock Text="{Binding Path=NumberOfCountries}" Grid.Column="1" Grid.Row="1" />
                    <TextBlock Text="Best city to be in" Grid.Column="0" Grid.Row="2" />
                    <TextBlock Text="{Binding Path=BestCityToBeIn}" Grid.Column="1" Grid.Row="2" />
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="readOnlyCountryDataTemplate">
                <DataTemplate.Resources>
                    <Style BasedOn="{StaticResource textBlockStyle}" TargetType="{x:Type TextBlock}" />
                </DataTemplate.Resources>
                <Grid VerticalAlignment="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Name" Grid.Column="0" Grid.Row="0" />
                    <TextBlock Text="{Binding Path=CountryName}" Grid.Column="1" Grid.Row="0" />
                    <TextBlock Text="Country size in square km" Grid.Column="0" Grid.Row="1" />
                    <TextBlock Text="{Binding Path=Size}" Grid.Column="1" Grid.Row="1" />
                    <TextBlock Text="Birth rate" Grid.Column="0" Grid.Row="2" />
                    <TextBlock Text="{Binding Path=BirthRate}" Grid.Column="1" Grid.Row="2" />
                </Grid>
            </DataTemplate>            
            <DataTemplate x:Key="readOnlyCityDataTemplate">
                <DataTemplate.Resources>
                    <Style BasedOn="{StaticResource textBlockStyle}" TargetType="{x:Type TextBlock}" />
                </DataTemplate.Resources>
                <Grid VerticalAlignment="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Name" Grid.Column="0" Grid.Row="0" />
                    <TextBlock Text="{Binding Path=CityName}" Grid.Column="1" Grid.Row="0" />
                    <TextBlock Text="City population" Grid.Column="0" Grid.Row="1" />
                    <TextBlock Text="{Binding Path=Population}" Grid.Column="1" Grid.Row="1" />
                    <TextBlock Text="Time zone" Grid.Column="0" Grid.Row="2" />
                    <DockPanel Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left">
                        <TextBlock Text="UTC " Margin="0,5,0,0"/>
                        <TextBlock Text="{Binding Path=TimeZone}" Margin="0,5,0,0" />
                        <TextBlock Text=":00" Margin="0,5,0,0" />
                    </DockPanel>
                </Grid>
            </DataTemplate>
            <local:ReadOnlyObjectsDataTemplateSelector
            x:Key="selector"
            ContinentDataTemplate="{StaticResource readOnlyContinentDataTemplate}"
            CountryDataTemplate="{StaticResource readOnlyCountryDataTemplate}"
            CityDataTemplate="{StaticResource readOnlyCityDataTemplate}"
            />
        </ResourceDictionary>        
    </Grid.Resources> 
    <Grid.RowDefinitions>
        <RowDefinition />        
    </Grid.RowDefinitions>
    <ContentControl ContentTemplateSelector="{StaticResource selector}" Content="{Binding}" />
</Grid>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
Software Developer in a promising Clean-Tech company

Comments and Discussions