Click here to Skip to main content
12,295,482 members (56,520 online)
Click here to Skip to main content


75 bookmarked

Item-Level Presentation Models for WPF

, 2 Apr 2011 GPL3
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 ) { = 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: ]

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.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Adrian Alexander
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.)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 2 Apr 2011
Article Copyright 2008 by Adrian Alexander
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid