Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / WPF

WPF CRUD Generator (Scaffolding)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (11 votes)
4 Jun 2009LGPL35 min read 88.2K   6.3K   61  
Semi-automatic GUI generation from Domain Models.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.Specialized;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;

namespace Technewlogic.WpfShell.UI.Controls
{
	public class SortListView : ListView
	{
		static SortListView()
		{
			DefaultStyleKeyProperty.OverrideMetadata(
				typeof(SortListView),
				new FrameworkPropertyMetadata(typeof(SortListView)));
		}

		public SortListView()
		{
			_headerClickHandler = new RoutedEventHandler(GridViewColumnHeader_Click);
		}

		private RoutedEventHandler _headerClickHandler;
		private ListSortDirection _lastDirection = ListSortDirection.Ascending;

		#region DefaultItemsContainerStyle

		public Style DefaultItemsContainerStyle
		{
			get { return (Style)GetValue(DefaultItemsContainerStyleProperty); }
			set { SetValue(DefaultItemsContainerStyleProperty, value); }
		}

		// Using a DependencyProperty as the backing store for DefaultItemsContainerStyle.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DefaultItemsContainerStyleProperty =
			DependencyProperty.Register("DefaultItemsContainerStyle", typeof(Style), typeof(SortListView),
			new UIPropertyMetadata(null, new PropertyChangedCallback((s, e) =>
				{
					var me = s as SortListView;
					me.ItemContainerStyle = e.NewValue as Style;
				})));

		#endregion

		public override void EndInit()
		{
			base.EndInit();

			var gridView = View as GridView;
			if (gridView != null)
				gridView.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(GridViewColumns_CollectionChanged);
		}

		void GridViewColumns_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach (var it in e.NewItems)
				{
					var header = GetGridViewColumnHeader(it);
					if (header != null)
						header.Click += _headerClickHandler;
				}
			}
			else if (e.Action == NotifyCollectionChangedAction.Remove)
			{
				foreach (var it in e.OldItems)
				{
					var header = GetGridViewColumnHeader(it);
					if (header != null)
						header.Click -= _headerClickHandler;
				}
			}
		}

		private GridViewColumnHeader GetGridViewColumnHeader(object gridViewColumn)
		{
			var column = gridViewColumn as GridViewColumn;
			if (column != null)
				return column.Header as GridViewColumnHeader;
			else
				return null;
		}

		void GridViewColumnHeader_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

			if (headerClicked != null)
			{
				if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
				{
					if (_lastDirection == ListSortDirection.Ascending)
						_lastDirection = ListSortDirection.Descending;
					else
						_lastDirection = ListSortDirection.Ascending;

					Sort(headerClicked.Name, _lastDirection);
				}
			}
		}

		private void Sort(string sortBy, ListSortDirection direction)
		{
			ICollectionView dataView = CollectionViewSource.GetDefaultView(ItemsSource);

			dataView.SortDescriptions.Clear();
			SortDescription sd = new SortDescription(sortBy, direction);
			dataView.SortDescriptions.Add(sd);
			dataView.Refresh();
		}
	}
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) www.technewlogic.de
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions