Click here to Skip to main content
13,149,099 members (55,784 online)
Click here to Skip to main content


75 bookmarked
Posted 24 Aug 2008

Item-Level Presentation Models for WPF

, 2 Apr 2011
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.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using HappyNomad.Examples.PresentationModel.GUI.Model;

namespace HappyNomad.Examples.PresentationModel.GUI {

	/// <summary>
	/// Interaction logic for Demo2.xaml
	/// </summary>
	public partial class Demo2 : UserControl {
		private SampleTreeNodeView rootWrapper;
		public Demo2() {

			//Build sample presentation-model objects:
			rootWrapper = new SampleTreeNodeView();
			sampleTreeView.ItemsSource = rootWrapper.SubItems; //SampleTreeNode.Root.SubNodes;

		private void addButton_Click( object sender, RoutedEventArgs args ) {
			SampleTreeNodeView selected = (SampleTreeNodeView)sampleTreeView.SelectedItem; //get selected item
			SampleTreeNode newItem = new SampleTreeNode( editItemBox.Text ); //create new item
			int newIndex = selected.Parent.SubItems.IndexOf( selected ) + 1; //determine new item's position

			//Insert new item into domain-model tree structure:
			selected.Parent.DomainItem.SubNodes.Insert( newIndex, newItem );

			//Select new item in UI:
			rootWrapper.FindDescendant( newItem ).IsSelected = true;

		private void updateButton_Click( object sender, RoutedEventArgs args ) {
			BindingExpression be = editItemBox.GetBindingExpression( TextBox.TextProperty );
			be.UpdateSource(); //update sampleTreeView.SelectedItem.DomainItem.Name with editItemBox.Text's value

		private void demoteButton_Click( object sender, RoutedEventArgs args ) {
			SampleTreeNodeView viewToMove = (SampleTreeNodeView)sampleTreeView.SelectedItem; //get selected item
			SampleTreeNode itemToMove = viewToMove.DomainItem; //get selected domain object

			SampleTreeNode oldParent = itemToMove.ParentNode; //Determine former parent...
			int oldIndex = oldParent.SubNodes.IndexOf( itemToMove ); //and index.
			if ( oldIndex == 0 ) return; //nowhere to demote to, so finished

			SampleTreeNode newParent = oldParent.SubNodes[oldIndex - 1]; //Determine new parent...
			int newIndex = newParent.SubNodes.Count; //and index.

			itemToMove.ParentNode = null; //move the selected item
			newParent.SubNodes.Insert( newIndex, itemToMove );

			viewToMove.Parent.IsExpanded = true; //Make sure the moved item is visible
			viewToMove.IsSelected = true; //and that it's selected.
			sampleTreeView.Focus(); //avoid some strange WPF behaviour

		private void deleteButton_Click( object sender, RoutedEventArgs args ) {
			SampleTreeNodeView selected = (SampleTreeNodeView)sampleTreeView.SelectedItem; //get selected item
			selected.DomainItem.ParentNode = null; //remove subtree

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...

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