Click here to Skip to main content
13,198,551 members (60,727 online)
Click here to Skip to main content

Stats

18.2K views
22 bookmarked
Posted 22 Mar 2010

MVVM for Multi Platforms

, 22 Mar 2010
How to implement MVVM when developing a view model whose view implementation language is not certain
GeographicRepresentation
bin
Release
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Interfaces.pdb
GeographicRepresentation.Lib.dll
GeographicRepresentation.Lib.pdb
GeographicRepresentation.TestLib.dll
GeographicRepresentation.TestLib.pdb
GeographicRepresentation.UILib.exe
GeographicRepresentation.UILib.pdb
GeographicRepresentation.UILib.vshost.exe
GeographicRepresentation.UILib.vshost.exe.manifest
Resources
GeographicRepresentation.suo
GeographicRepresentation.TestLib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
GeographicRepresentation.TestLib.dll
Release
Properties
GeographicRepresentation.vsmdi
Interfaces
bin
Debug
GeographicRepresentation.Interfaces.dll
Release
Properties
Lib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
Release
Model
Properties
Resources
ViewModel
LocalTestRun.testrunconfig
TestResults
UILib
bin
Debug
GeographicRepresentation.UILib.exe
WpfApplication1.vshost.exe.manifest
Release
GeographicRepresentation.UILib.vshost.exe.manifest
Properties
UILib.csproj.user
View
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

using GeographicRepresentation.Interfaces;

namespace GeographicRepresentation.UILib.View
{
    /// <summary>
    /// The following class is used for selecting a suitable data template for each of the tree view items
    /// A data template selector is used since the items are interfaces and not concrete classes
    /// </summary>
    public class ReadOnlyObjectsDataTemplateSelector : DataTemplateSelector
    {
        #region Members

        DataTemplate m_ContinentDataTemplate;
        DataTemplate m_CountryDataTemplate;
        DataTemplate m_CityDataTemplate;        

        #endregion

        #region Properties

        public DataTemplate ContinentDataTemplate
        {
            get { return m_ContinentDataTemplate; }
            set { m_ContinentDataTemplate = value; }
        }

        public DataTemplate CountryDataTemplate
        {
            get { return m_CountryDataTemplate; }
            set { m_CountryDataTemplate = value; }
        }

        public DataTemplate CityDataTemplate
        {
            get { return m_CityDataTemplate; }
            set 
            { 
                m_CityDataTemplate = value; 
            }
        }

        #endregion

        #region Methods

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            DataTemplate dTemplate = null;
            if (item != null)
            {
                Type[] types = item.GetType().GetInterfaces();
                if (types.Contains(typeof(IReadOnlyContinentViewModel)))
                {
                    dTemplate = ContinentDataTemplate;
                }
                else if (types.Contains(typeof(IReadonlyCountryViewModel)))
                {
                    dTemplate = CountryDataTemplate;
                }
                else if (types.Contains(typeof(IReadOnlyCityViewModel)))
                {
                    dTemplate = CityDataTemplate;
                }
            }

            return dTemplate;
        }

        #endregion
    }
}

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)

Share

About the Author

Izhar Lotem
Software Developer
Israel Israel
Software Developer in a promising Clean-Tech company

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 22 Mar 2010
Article Copyright 2010 by Izhar Lotem
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid