Click here to Skip to main content
12,551,373 members (51,099 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;

namespace HappyNomad.BidirectionalAssociations {
	/// <summary>
	/// Keeps both sides of a bidirectional many-to-many association in sync with each other.
	/// </summary>
	/// <author>Adrian Alexander</author>
	public sealed class ManyToManyAssocSync<T> {
		private T thisSide;
		private string otherSidePropertyName;
		private PropertyInfo otherSideProperty;

		public ManyToManyAssocSync( T thisSide, string otherSideToThisSidePropertyName ) {
			this.thisSide = thisSide;
			this.otherSidePropertyName = otherSideToThisSidePropertyName;

		/// <summary>
		/// Responds to add/remove events raised by this side's collection.
		/// </summary>
		public void UpdateOtherSide( object sender, NotifyCollectionChangedEventArgs e ) {
			if ( e.Action == NotifyCollectionChangedAction.Add )
				//addingToOtherSide: the item that was just added to this side's collection
				foreach ( object addingToOtherSide in e.NewItems ) {
					ICollection<T> otherSidesCollection = GetOtherSidesCollection( addingToOtherSide );
					if ( ReflectionUtil.IsInitialized(otherSidesCollection) && !otherSidesCollection.Contains(thisSide) ) {
						if ( otherSidesCollection is IList && otherSidesCollection.Contains(thisSide) ) // true if add will cause duplicates
							throw new InvalidOperationException( "Collection is non-unique" );
						otherSidesCollection.Add( thisSide );
			else if ( e.Action == NotifyCollectionChangedAction.Remove )
				//removingFromOtherSide: the item that was just removed from this side's collection
				foreach ( object removingFromOtherSide in e.OldItems ) {
					System.Console.WriteLine( "old item removed from set" );
					ICollection<T> otherSidesCollection = GetOtherSidesCollection( removingFromOtherSide );
					if ( ReflectionUtil.IsInitialized(otherSidesCollection) && otherSidesCollection.Contains(thisSide) ) {
						otherSidesCollection.Remove( thisSide );
						if ( otherSidesCollection is IList && otherSidesCollection.Contains(thisSide) ) // true if there was more than one
							throw new InvalidOperationException( "Collection is non-unique" );

		private ICollection<T> GetOtherSidesCollection( object otherSide ) {
			if ( otherSideProperty == null )
				otherSideProperty = otherSide.GetType().GetProperty( otherSidePropertyName );
			return (ICollection<T>)otherSideProperty.GetValue( otherSide, null );

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
Web01 | 2.8.161021.1 | Last Updated 2 Apr 2011
Article Copyright 2008 by Adrian Alexander
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid