Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Modeling M-V-VM

, 5 Jun 2009
An article which tries to present an approach for decoupling M-V-VM View Model objects
TestTreeViewWithLeftBarView.zip
TestTreeViewWithLeftBarView
bin
Debug
TestTreeViewWithLeftBarView.exe
TestTreeViewWithLeftBarView.vshost.exe
TestTreeViewWithLeftBarView.vshost.exe.manifest
WorldDataLib.dll
Properties
Settings.settings
Resources
Service References
WorldServiceReference
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
service.wsdl
View
ViewModel
WorldDataLib
bin
Debug
WorldDataLib.dll
Model
Properties
WorldServiceApp
bin
Debug
WorldDataLib.dll
WorldServiceApp.exe
WorldServiceApp.vshost.exe
WorldServiceApp.vshost.exe.manifest
WorldServiceLib.dll
Properties
WorldServiceLib
bin
Debug
WorldDataLib.dll
WorldServiceLib.dll
Properties
WorldServiceLib.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.ComponentModel;

using WorldDataLib.Model;

namespace TestTreeViewWithRightBarView.ViewModel
{
     public class CityViewModel : ITreeViewNode, ICityViewModel, ISelectionSectionPresenter, INotifyPropertyChanged
     {
         #region Members

         IViewModel m_Parent;
         City m_City;

         #endregion

         #region Properties

         #region ITreeViewNode Members

         public string Caption
         {
             get 
             {
                 string str = string.Empty;
                 if (m_City != null)
                 {
                     str = m_City.Name;
                 }
                 return str; 
             }
         }        
        
         #endregion

         #region IViewModel Members

         public IViewModel VMParent
         {
             get 
             {
                 return m_Parent; 
             }
         }

         #endregion

         #region ISelectionSectionPresenter Members

         public string Title
         {
             get;
             set;
         }

         #endregion

         public double Temprature
         {
             get;
             set;
         }

         #endregion

         #region Methods   
     
         #region Ctor

         public CityViewModel(IViewModel parent, City city)
         {
             m_Parent = parent;
             m_City = city;
         }

         #endregion

         #region IViewModel Members

        public void HandleEvent(IViewModel sender, object param, ViewModelOperationsEnum viewModelEvent)
        {
            if (m_Parent != null)
            {
                m_Parent.HandleEvent(sender, param, viewModelEvent);
            }
        }

        #endregion

         #region GotFocus

         public ICommand GotFocus
         {
             get
             {
                 return new DelegateCommand(this.ControlGotFocus);
             }
         }

         /// <summary>
        /// When the control gets focus, an event is fired for external handling
        /// </summary>
        public void ControlGotFocus()
        {
            if (VMParent != null)
            {
                VMParent.HandleEvent(this, null, ViewModelOperationsEnum.GotFocus);
                VMParent.HandleEvent(this, Caption, ViewModelOperationsEnum.GetCityTemprature);
            }
        }

        #endregion

         #region ICityViewModel Members

        public void SetTemprature(double fTemprature)
        {
            Temprature = fTemprature;
            Title = string.Format("City: {0}. Current temprature: {1}", Caption, Temprature);
            OnPropertyChanged("Title");
        }         

        #endregion

        #endregion        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string str)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(str));
            }
        }

        #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)

About the Author

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 5 Jun 2009
Article Copyright 2009 by Izhar Lotem
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid