Click here to Skip to main content
15,886,026 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 94.2K   1.5K   176  
Design patterns on the presentation layer for WPF, Silverlight and Windows Phone applications.
// -- FILE ------------------------------------------------------------------
// name       : ModelDataGrid.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.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Itenso.Community.XamlPatterns.Windows;
using ICommand = Itenso.Community.XamlPatterns.Windows.Input.ICommand;

namespace Itenso.Community.XamlPatterns.Controls
{

	// ------------------------------------------------------------------------
	public class ModelDataGrid : DataGrid, IDisposable
	{

		// ----------------------------------------------------------------------
		public static readonly DependencyProperty ItemCreateCommandProperty = DependencyProperty.RegisterAttached(
			"ItemCreateCommand",
			typeof( ICommand ),
			typeof( ModelDataGrid ),
			null );

		// ----------------------------------------------------------------------
		public static readonly DependencyProperty ItemEditCommandProperty = DependencyProperty.RegisterAttached(
			"ItemEditCommand",
			typeof( ICommand ),
			typeof( ModelDataGrid ),
			new PropertyMetadata( ItemEditCommandPropertyChanged ) );

		// ----------------------------------------------------------------------
		public static readonly DependencyProperty ItemDeleteCommandProperty = DependencyProperty.RegisterAttached(
			"ItemDeleteCommand",
			typeof( ICommand ),
			typeof( ModelDataGrid ),
			null );

		// ----------------------------------------------------------------------
		public ICommand ItemCreateCommand
		{
			get { return (ICommand)GetValue( ItemCreateCommandProperty ); }
			set { SetValue( ItemCreateCommandProperty, value ); }
		} // ItemCreateCommand

		// ----------------------------------------------------------------------
		public ICommand ItemEditCommand
		{
			get { return (ICommand)GetValue( ItemEditCommandProperty ); }
			set { SetValue( ItemEditCommandProperty, value ); }
		} // ItemEditCommand

		// ----------------------------------------------------------------------
		public ICommand ItemDeleteCommand
		{
			get { return (ICommand)GetValue( ItemDeleteCommandProperty ); }
			set { SetValue( ItemDeleteCommandProperty, value ); }
		} // ItemDeleteCommand

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

		// ----------------------------------------------------------------------
		protected virtual void Dispose( bool disposing )
		{
			if ( disposing )
			{
				if ( mouseClickManager != null )
				{
					mouseClickManager.DoubleClick -= MouseDoubleClickHandler;
				}
			}
		} // Dispose

		// ----------------------------------------------------------------------
		protected override void OnKeyDown( KeyEventArgs e )
		{
			base.OnKeyDown( e );

			if ( Keyboard.Modifiers != ModifierKeys.None )
			{
				return;
			}

			switch ( e.Key )
			{
				case Key.Insert:
					e.Handled = HandleCommandRequest( ItemCreateCommand );
					break;
				case Key.F2:
					e.Handled = HandleCommandRequest( ItemEditCommand );
					break;
				case Key.Delete:
					e.Handled = HandleCommandRequest( ItemDeleteCommand );
					break;
			}
		} // OnKeyDown

		// ----------------------------------------------------------------------
		protected override void OnMouseLeftButtonUp( MouseButtonEventArgs e )
		{
			base.OnMouseLeftButtonUp( e );

			if ( mouseClickManager != null )
			{
				mouseClickManager.HandleClick( e );
			}
		} // OnMouseLeftButtonUp

		// ----------------------------------------------------------------------
		protected bool HandleCommandRequest( ICommand command, object parameter = null )
		{
			if ( command == null )
			{
				return false;
			}

			if ( !command.CanExecute( parameter ) )
			{
				return false;
			}

			command.Execute( parameter );
			return true;
		} // HandleCommandRequest

		// ----------------------------------------------------------------------
		private void SetupItemEditCommand()
		{
			if ( ItemEditCommand == null || mouseClickManager != null )
			{
				return;
			}

			mouseClickManager = new MouseClickManager( this );
			mouseClickManager.DoubleClick += MouseDoubleClickHandler;
		} // SetupItemEditCommand

		// ----------------------------------------------------------------------
		private void MouseDoubleClickHandler( object sender, MouseButtonEventArgs e )
		{
			HandleCommandRequest( ItemEditCommand );
		} // MouseDoubleClickHandler

		// ----------------------------------------------------------------------
		private static void ItemEditCommandPropertyChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e )
		{
			((ModelDataGrid)dependencyObject).SetupItemEditCommand();
		} // ItemEditCommandPropertyChanged

		// ----------------------------------------------------------------------
		// members
		private MouseClickManager mouseClickManager;

	} // class ModelDataGrid

} // namespace Itenso.Community.XamlPatterns.Controls
// -- 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