Click here to Skip to main content
15,881,413 members
Articles / Mobile Apps / Windows Phone 7

Presentation Patterns for XAML based Applications

Rate me:
Please Sign up or sign in to vote.
4.99/5 (44 votes)
17 Sep 2013CPOL23 min read 93.4K   1.5K   176  
Design patterns on the presentation layer for WPF, Silverlight and Windows Phone applications.
// -- FILE ------------------------------------------------------------------
// name       : CustomerAdminEditor.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.Collections.Generic;
using System.Windows;
using Itenso.Community.XamlPatterns.Collection;
using Itenso.Community.XamlPatterns.Program.Presentation.ViewModel;
using Itenso.Community.XamlPatterns.Presentation;

namespace Itenso.Community.XamlPatterns.Program.Examples.CustomerAdmin.Editor
{

	// ------------------------------------------------------------------------
	public class CustomerAdminEditor : ItemCollectionEditor<ItemCollection<CustomerModel>, CustomerModel>
	{

		// ----------------------------------------------------------------------
		public CustomerAdminEditor( ItemCollection<CustomerModel> items, ViewSyncMode syncMode ) :
			base( items, syncMode )
		{
		} // CustomerAdminEditor

		// ----------------------------------------------------------------------
		public IEnumerable<ItemInsertMode> ItemInsertModes
		{
			get
			{
				return itemInsertModes ?? ( itemInsertModes = new List<ItemInsertMode>
				                                            {
				                                            	ItemInsertMode.Current,
																											ItemInsertMode.Next,
				                                            	ItemInsertMode.Append
				                                            } );
			}
		} // ItemInsertModes

		// ----------------------------------------------------------------------
		public ItemInsertMode InsertMode
		{
			get { return insertMode; }
			set
			{
				if ( value != insertMode )
				{
					insertMode = value;
					NotifyPropertyChanged( "InsertMode" );
				}
			}
		} // InsertMode

		// ----------------------------------------------------------------------
		protected override void UpdateCommands()
		{
			base.UpdateCommands();

			CanCreate = true;
			CanEdit = View.CurrentItem != null;
			CanDelete = View.CurrentItem != null;
		} // UpdateCommands

		// ----------------------------------------------------------------------
		public override void Create( Action onFinished )
		{
			CustomerModel newItem = new CustomerModel( "New First Name", "New LastName", "New Address", true, true );

			ItemInsertMode mode = InsertMode;
			if ( View.CurrentItem == null )
			{
				mode = ItemInsertMode.Append;
			}
			switch ( mode )
			{
				case ItemInsertMode.Current:
					Items.Insert( View.CurrentPosition, newItem );
					break;
				case ItemInsertMode.Next:
					Items.Insert( View.CurrentPosition + 1, newItem );
					break;
				case ItemInsertMode.Append:
					Items.Add( newItem );
					break;
			}

			MessageBox.Show(
				string.Format( "Customer created:\n{0} [{1}] at #{2}", newItem.LastName, newItem.LastModified, Items.IndexOf( newItem ) ),
				messageTitle,
				MessageBoxButton.OK );

			base.Create( onFinished );
		} // Create

		// ----------------------------------------------------------------------
		public override void Edit( Action onFinished )
		{
			DateTime oldLastModified = View.CurrentItem.LastModified;
			View.CurrentItem.LastModified = DateTime.Now;

			MessageBox.Show(
				string.Format( "Updated 'Last Modified'\n\nOld value: {0}\nNew value: {1}", oldLastModified, View.CurrentItem.LastModified ),
				messageTitle,
				MessageBoxButton.OK );

			base.Edit( onFinished );
		} // Edit

		// ----------------------------------------------------------------------
		public override void Delete( Action onFinished )
		{
			CustomerModel deleteItem = View.CurrentItem;

			if ( MessageBox.Show( "Delete " + deleteItem.LastName + "?", messageTitle, MessageBoxButton.OKCancel ) == MessageBoxResult.OK )
			{
				Items.Remove( deleteItem );

				MessageBox.Show(
					string.Format( "Customer deleted:\n{0} [{1}]", deleteItem.LastName, deleteItem.LastModified ),
					messageTitle,
					MessageBoxButton.OK );
			}

			base.Delete( onFinished );
		} // Delete

		// ----------------------------------------------------------------------
		private IEnumerable<ItemInsertMode> itemInsertModes;
		private ItemInsertMode insertMode;

		private const string messageTitle = "Customer Editor";

	} // class CustomerAdminEditor

} // namespace Itenso.Community.XamlPatterns.Program.Examples.CustomerAdmin.Editor
// -- 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)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions