Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Presentation Patterns for XAML based Applications

, 17 Sep 2013 CPOL
Design patterns on the presentation layer for WPF, Silverlight and Windows Phone applications.
XamlPatterns_v1.1.0.0.zip
Program
App.Desktop.ico
App.Silverlight.png
App.WindowsPhone.png
Background.WindowsPhone.png
Examples
AssemblyBrowser
View
ViewModel
AssemblyBrowser.cd
CustomerAdmin
Editor
View
ViewModel
GridViewBinding
View
ViewModel
ListBoxBinding
View
ViewModel
MediaPlayer
Images
Pause.png
Play.png
Resume.png
Stop.png
View
ViewModel
OrderBrowser
Editor
Images
Companies.png
Company.png
Customer.png
Customers.png
Order.png
OrderItem.png
OrderItems.png
Orders.png
View
ViewModel
OrderBrowser.cd
Selection
View
ViewModel
obj
x86
Debug
TempPE
Presentation
ViewModel
Dependency
Notifiable
Program.Silverlight.suo
Properties
SplashScreenImage.jpg
Videos
SampleVideo.wmv
WMA_WMAV1_wmaV1.wma
Controls
obj
Debug
TempPE
Properties
Core
Actions
Data
Item
Media
Navigation
Collection
Commands
Data
Edit
Media
Navigation
Diagnostics
obj
Debug
TempPE
Presentation
Dependency
ItemModel.cd
Notifiable
Properties
Windows
Converter
Input
docu
Performance.xls
XamlPatterns_en.pdf
Pub
Desktop.Debug
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
Desktop.Release
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
Silverlight.Debug
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
Silverlight.Release
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
WindowsPhone.Debug
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
WindowsPhone.Release
Itenso.Community.XamlPatterns.Controls.dll
Itenso.Community.XamlPatterns.dll
XamlPatterns.Desktop.suo
XamlPatterns.Silverlight.suo
XamlPatterns.WindowsPhone.suo
// -- FILE ------------------------------------------------------------------
// name       : ItemEditor.cs
// project    : Itenso Community
// created    : Jani Giannoudis - 2012.05.05
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2004-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.ComponentModel;
using Itenso.Community.XamlPatterns.Actions.Data;
using Itenso.Community.XamlPatterns.Commands.Edit;
using Itenso.Community.XamlPatterns.Windows.Input;

namespace Itenso.Community.XamlPatterns.Presentation
{

	// ------------------------------------------------------------------------
	public abstract class ItemEditor : IItemEditor
	{

		// ----------------------------------------------------------------------
		public event EventHandler ItemChanged;

		// ----------------------------------------------------------------------
		public event PropertyChangedEventHandler PropertyChanged;

		// ----------------------------------------------------------------------
		public event EventHandler CanCreateChanged;

		// ----------------------------------------------------------------------
		public event EventHandler CanEditChanged;

		// ----------------------------------------------------------------------
		public event EventHandler CanDeleteChanged;

		// ----------------------------------------------------------------------
		public event EventHandler CanRefreshChanged;

		// ----------------------------------------------------------------------
		protected ItemEditor()
		{
			itemCreateCommand = commands.Register( new ItemCreateCommand( this ) );
			itemEditCommand = commands.Register( new ItemEditCommand( this ) );
			itemDeleteCommand = commands.Register( new ItemDeleteCommand( this ) );
			itemRefreshCommand = commands.Register( new ItemRefreshCommand( this ) );
		} // ItemEditor

		// ----------------------------------------------------------------------
		public virtual object Item
		{
			get { return item; }
			set
			{
				if ( !Equals( item, value ) )
				{
					item = value;
					NotifyItemChanged();
				}
			}
		} // Item

		#region Command

		// ----------------------------------------------------------------------
		public virtual ItemCreateCommand ItemCreateCommand
		{
			get { return itemCreateCommand; }
		} // ItemCreateCommand

		// ----------------------------------------------------------------------
		public virtual ItemEditCommand ItemEditCommand
		{
			get { return itemEditCommand; }
		} // ItemEditCommand

		// ----------------------------------------------------------------------
		public virtual ItemDeleteCommand ItemDeleteCommand
		{
			get { return itemDeleteCommand; }
		} // ItemDeleteCommand

		// ----------------------------------------------------------------------
		public virtual ItemRefreshCommand ItemRefreshCommand
		{
			get { return itemRefreshCommand; }
		} // ItemRefreshCommand

		#endregion

		#region Action Status

		// ----------------------------------------------------------------------
		public virtual bool CanCreate
		{
			get { return canCreate; }
			protected set
			{
				if ( value != canCreate )
				{
					canCreate = value;
					NotifyPropertyChanged( "CanCreate" );
					OnCanCreateChanged();
				}
			}
		} // CanCreate

		// ----------------------------------------------------------------------
		public virtual bool CanEdit
		{
			get { return canEdit; }
			protected set
			{
				if ( value != canEdit )
				{
					canEdit = value;
					NotifyPropertyChanged( "CanEdit" );
					OnCanEditChanged();
				}
			}
		} // CanEdit

		// ----------------------------------------------------------------------
		public virtual bool CanDelete
		{
			get { return canDelete; }
			protected set
			{
				if ( value != canDelete )
				{
					canDelete = value;
					NotifyPropertyChanged( "CanDelete" );
					OnCanDeleteChanged();
				}
			}
		} // CanDelete

		// ----------------------------------------------------------------------
		public virtual bool CanRefresh
		{
			get { return canRefresh; }
			protected set
			{
				if ( value != canRefresh )
				{
					canRefresh = value;
					NotifyPropertyChanged( "CanRefresh" );
					OnCanRefreshChanged();
				}
			}
		} // CanRefresh

		#endregion

		#region Action

		// ----------------------------------------------------------------------
		public virtual void Create( Action onFinished )
		{
			if ( onFinished != null )
			{
				onFinished();
			}
		} // Create

		// ----------------------------------------------------------------------
		public virtual void Edit( Action onFinished )
		{
			if ( onFinished != null )
			{
				onFinished();
			}
		} // Edit

		// ----------------------------------------------------------------------
		public virtual void Delete( Action onFinished )
		{
			if ( onFinished != null )
			{
				onFinished();
			}
		} // Delete

		// ----------------------------------------------------------------------
		public virtual void Refresh( Action onFinished )
		{
			IRefreshable refreshable = Item as IRefreshable;
			if ( refreshable != null )
			{
				refreshable.Refresh();
			}
			if ( onFinished != null )
			{
				onFinished();
			}
		} // Refresh

		#endregion

		#region Dispose

		// ----------------------------------------------------------------------
		public void Dispose()
		{
			Dispose( true );
			GC.SuppressFinalize( this );
		} // Dispose

		// ----------------------------------------------------------------------
		protected virtual void Dispose( bool disposing )
		{
			if ( disposing )
			{
				commands.Dispose();
			}
		} // Dispose

		#endregion

		// ----------------------------------------------------------------------
		protected abstract void UpdateCommands();

		// ----------------------------------------------------------------------
		protected void RaiseUpdateCommands()
		{
			UpdateCommands();
		} // RaiseUpdateCommands

		#region Events

		// ----------------------------------------------------------------------
		protected virtual void OnCanCreateChanged()
		{
			EventHandler canCreateChanged = CanCreateChanged;
			if ( canCreateChanged != null )
			{
				canCreateChanged( this, EventArgs.Empty );
			}
		} // OnCanCreateChanged

		// ----------------------------------------------------------------------
		protected virtual void OnCanEditChanged()
		{
			EventHandler canEditChanged = CanEditChanged;
			if ( canEditChanged != null )
			{
				canEditChanged( this, EventArgs.Empty );
			}
		} // OnCanEditChanged

		// ----------------------------------------------------------------------
		protected virtual void OnCanDeleteChanged()
		{
			EventHandler canDeleteChanged = CanDeleteChanged;
			if ( canDeleteChanged != null )
			{
				canDeleteChanged( this, EventArgs.Empty );
			}
		} // OnCanDeleteChanged

		// ----------------------------------------------------------------------
		protected virtual void OnCanRefreshChanged()
		{
			EventHandler canRefrehChanged = CanRefreshChanged;
			if ( canRefrehChanged != null )
			{
				canRefrehChanged( this, EventArgs.Empty );
			}
		} // OnCanRefreshChanged

		// ----------------------------------------------------------------------
		protected virtual void OnItemChanged()
		{
			EventHandler itemChanged = ItemChanged;
			if ( itemChanged != null )
			{
				itemChanged( this, EventArgs.Empty );
			}
		} // OnItemChanged

		#endregion

		// ----------------------------------------------------------------------
		protected void NotifyItemChanged()
		{
			NotifyPropertyChanged( "Item" );
			OnItemChanged();
			RaiseUpdateCommands();
		} // NotifyItemChanged

		// ----------------------------------------------------------------------
		protected void NotifyPropertyChanged( string propertyName )
		{
			PropertyTool.RaisePropertyChanged( this, propertyName, PropertyChanged );
		} // NotifyPropertyChanged

		// ----------------------------------------------------------------------
		// members
		private readonly CommandCollection commands = new CommandCollection();
		private readonly ItemCreateCommand itemCreateCommand;
		private readonly ItemEditCommand itemEditCommand;
		private readonly ItemDeleteCommand itemDeleteCommand;
		private readonly ItemRefreshCommand itemRefreshCommand;
		private bool canCreate;
		private bool canEdit;
		private bool canDelete;
		private bool canRefresh;
		private object item;

	} // class ItemEditor

} // namespace Itenso.Community.XamlPatterns.Presentation
// -- EOF -------------------------------------------------------------------

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)

Share

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 18 Sep 2013
Article Copyright 2012 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid