Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

Modeling M-V-VM

Rate me:
Please Sign up or sign in to vote.
3.71/5 (8 votes)
5 Jun 2009CPOL2 min read 26.9K   312   23  
An article which tries to present an approach for decoupling M-V-VM View Model objects
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.ComponentModel;

using WorldDataLib.Model;

namespace TestTreeViewWithRightBarView.ViewModel
{
    public class ContinentViewModel : ITreeViewWithChildrenNode, IContinentViewModel, ISelectionSectionPresenter, INotifyPropertyChanged
    {
        #region Members

        IViewModel m_Parent;
        Continent m_Continent;

        ObservableCollection<ITreeViewNode> m_Children;

        #endregion

        #region Properties
        
        #region ITreeViewWithChildrenNode Members

        public ObservableCollection<ITreeViewNode> Children
        {
            get 
            {
                return m_Children; 
            }
        }

        #endregion

        #region ITreeViewNode Members

        public string Caption
        {
            get 
            {
                string str = string.Empty;
                if (m_Continent != null)
                {
                    str = m_Continent.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 Size
        {
            get;
            set;
        }

        #endregion

        #region Methods

        #region Ctor

        public ContinentViewModel(IViewModel parent, Continent continent)
        {
            m_Parent = parent;
            m_Continent = continent;
            m_Children = new ObservableCollection<ITreeViewNode>();
            if ((m_Continent != null) && (m_Continent.Countries != null))
            {
                for (int iter = 0; iter < m_Continent.Countries.Count; iter++)
                {
                    m_Children.Add(new CountryViewModel(this, m_Continent.Countries[iter]));
                }
            }
        }

        #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.GetContinentSize);
            }
        }

        #endregion

        #region IContinentViewModel Members

        public void SetSize(double fSize)
        {
            Size = fSize;
            Title = string.Format("Continent {0} size is {1}sq miles",Caption, Size);
            OnPropertyChanged("Title");
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

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

        #endregion

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


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

Comments and Discussions