Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / WPF

Item-Level Presentation Models for WPF

Rate me:
Please Sign up or sign in to vote.
4.65/5 (18 votes)
2 Apr 2011GPL39 min read 80.3K   74  
Make your life easier by inserting a Presentation Model layer (aka ViewModel) between your domain-model collection contents and template-generated WPF objects.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

using HappyNomad.BidirectionalAssociations;

namespace HappyNomad.Examples.PresentationModel {

	public class SampleTreeNode {
		private string name;
		private IList<SampleTreeNode> subNodes;
		private SampleTreeNode parentNode;

		public SampleTreeNode( string name ) { this.name = name; }

		public string Name {
			get { return name; }
			set { name = value; }
		}

		public IList<SampleTreeNode> SubNodes {
			get {
				if ( subNodes == null ) {
					subNodes = new ObservableCollection<SampleTreeNode>();
					( (INotifyCollectionChanged)subNodes ).CollectionChanged +=
						new OneToManyAssocSync( this, "ParentNode" ).UpdateManySide;
				}
				return subNodes;
			}
		}

		public SampleTreeNode ParentNode {
			get { return parentNode; }
			set {
				SampleTreeNode oldParentNode = parentNode;
				parentNode = value;
				OneToManyAssocSync.UpdateOneSide( this, oldParentNode, parentNode, "SubNodes" );
			}
		}

		public override string ToString() {
			return GetType().Name + ": Name=\"" + Name + "\"";
		}

		public static SampleTreeNode Root {
			get {
				//Build sample domain-model data:
				SampleTreeNode reptilia = new SampleTreeNode( "Reptilia" );
				SampleTreeNode anapsida = new SampleTreeNode( "Anapsida" );
				SampleTreeNode parapsida = new SampleTreeNode( "Parapsida" );
				SampleTreeNode diapsida = new SampleTreeNode( "Diapsida" );
				SampleTreeNode synapsida = new SampleTreeNode( "Synapsida" );
				reptilia.SubNodes.Add( anapsida );
				reptilia.SubNodes.Add( parapsida );
				reptilia.SubNodes.Add( diapsida );
				reptilia.SubNodes.Add( synapsida );
				anapsida.SubNodes.Add( new SampleTreeNode("Captorhinida") );
				anapsida.SubNodes.Add( new SampleTreeNode("Chelonia") );
				parapsida.SubNodes.Add( new SampleTreeNode("Ichthyosauria") );
				parapsida.SubNodes.Add( new SampleTreeNode("Plesiosauria") );
				SampleTreeNode lepidosauria = new SampleTreeNode( "Lepidosauria" );
				SampleTreeNode archosauria = new SampleTreeNode( "Archosauria" );
				diapsida.SubNodes.Add( lepidosauria );
				diapsida.SubNodes.Add( archosauria );
				lepidosauria.SubNodes.Add( new SampleTreeNode("Sephenodonta") );
				lepidosauria.SubNodes.Add( new SampleTreeNode("Squamata") );
				archosauria.SubNodes.Add( new SampleTreeNode("Crocodylia") );
				archosauria.SubNodes.Add( new SampleTreeNode("Saurischia") );
				archosauria.SubNodes.Add( new SampleTreeNode("Ornithischia") );
				archosauria.SubNodes.Add( new SampleTreeNode("Pterosauria") );
				synapsida.SubNodes.Add( new SampleTreeNode("Pelycosauria") );
				synapsida.SubNodes.Add( new SampleTreeNode("Therapsida") );
				return reptilia;
				//[Source: http://www-biol.paisley.ac.uk/Courses/Tatner/biomedia/units/rept10.htm ]
			}
		}
	}
}

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 GNU General Public License (GPLv3)


Written By
United States United States
Adrian loves facilitating suave user experiences via the latest and greatest GUI technologies such as Windows 8 Metro-style apps as well as WPF. More generally, he finds joy in architecting software that is easy to comprehend and maintain. He does so by applying design patterns at the top-level, and by incessantly refactoring code at lower levels. He's always interested in hearing about opportunities for full or part-time development work. He resides in Pennsylvania but can potentially travel anywhere in the country. (Writing about himself in the third-person is Adrian's new hobby.)

Comments and Discussions