Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C#

Declarative Programming Of The MVC Pattern Within The Context Of DataBinding

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
31 May 200410 min read 88K   311   90  
Exploring the MVC pattern.
/*
 * Copyright (c) 2004 Marc Clifton
 * All Rights Reserved
 * 
 * License: GNU General Public License, see doc\license.txt
 * http://www.gnu.org/licenses/licenses.html#GPL
 * Owner: Marc Clifton email: webmaster@knowledgeautomation.com
 * First release published:
 * http://www.codeproject.com/cs/miscctrl/xmlGuiGenerator.asp
 * 2/25/04
*/

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace MyXaml.Extensions
{
	public class ItemInfo
	{
		protected Control style;
		protected int height;

		public Control Style
		{
			get {return style;}
		}

		public int Height
		{
			get {return height;}
			set {height=value;}
		}

		public ItemInfo(Control style, int height)
		{
			this.style=style;
			this.height=height;
		}
	}

	public class StyledListBox : ListBox
	{
		protected string visualStyle;
		protected Hashtable itemCollection;
		protected int margin;
		protected Control style;

		public string VisualStyle
		{
			get {return visualStyle;}
			set
			{
				visualStyle=value;
				style=(Control)Parser.CurrentInstance.CreateForm(visualStyle, null, null);
			}
		}

		public int Margin
		{
			get {return margin;}
			set {margin=value;}
		}

		public StyledListBox()
		{
			itemCollection=new Hashtable();
			DrawMode=DrawMode.OwnerDrawVariable;
		}

		public string SubItem(int n)
		{
			Control style=((ItemInfo)itemCollection[SelectedIndex]).Style;
			string text=GetText(style.Controls[n], SelectedIndex);
			return text;
		}

		protected override void OnSizeChanged(EventArgs e)
		{
			foreach(DictionaryEntry entry in itemCollection)
			{
				int index=(int)entry.Key;
				ItemInfo info=(ItemInfo)entry.Value;
				info.Height=GetItemHeight(info.Style, index);
				RefreshItem(index);
			}
		}

		protected override void OnMeasureItem(MeasureItemEventArgs e)
		{
			int height=0;
			if (itemCollection.Contains(e.Index))
			{
				height=((ItemInfo)itemCollection[e.Index]).Height;
			}
			else
			{
				if (visualStyle != null)
				{
					height=GetItemHeight(style, e.Index);
					itemCollection.Add(e.Index, new ItemInfo(style, height));
				}
			}
			e.ItemHeight=height+margin;
		}


		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			if (e.Index != -1)
			{
				if ((e.State & DrawItemState.Selected)==0)
				{
					e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window), e.Bounds);
				}
				else
				{
					Color colorStart=Color.FromArgb(255, 245, 200);
					Color colorEnd=Color.FromArgb(255, 210, 128);
					e.Graphics.FillRectangle(new LinearGradientBrush(e.Bounds, colorStart, colorEnd, 90), e.Bounds);

					// for some reason, there is a solid line going across the top of the selection!
					Rectangle r2=e.Bounds;
					r2.Height=2;
					e.Graphics.FillRectangle(new SolidBrush(colorStart), r2);
				}

				string text;
				Rectangle r=e.Bounds;
				Control style=((ItemInfo)itemCollection[e.Index]).Style;
				foreach(Control ctrl in style.Controls)
				{
					int height;
					GetText(e.Graphics, ctrl, e.Index, out text, out height);
				
					r.Height=height;
					e.Graphics.DrawString(text, ctrl.Font, new SolidBrush(ctrl.ForeColor), r);
					r.Y=r.Y+height;

				}
			}
		}

		protected int GetItemHeight(Control style, int index)
		{
			int totalHeight=0;
			Graphics gr=Graphics.FromHwnd(style.Handle);
			foreach(Control ctrl in style.Controls)
			{
				string text;
				int height;
				GetText(gr, ctrl, index, out text, out height);
				totalHeight+=height;
			}
			return totalHeight;
		}

		protected void GetText(Graphics gr, Control ctrl, int index, out string text, out int height)
		{
			text=ctrl.Text;
			height=0;
			try
			{
				bool wordWrap=false;
				wordWrap=((TextBox)ctrl).WordWrap;
				string dataSourceName=StringHelpers.LeftOf(text, ';').Trim();
				string field=StringHelpers.RightOf(text, '=').Trim();
				Parser parser=Parser.CurrentInstance;
				object ds=parser.GetReference(dataSourceName);
				DataTable dt=null;
				TypeConverter targetTypeConverter=TypeDescriptor.GetConverter(ds.GetType());
				if (targetTypeConverter.CanConvertTo(typeof(DataTable)))
				{
					dt=(DataTable)targetTypeConverter.ConvertTo(ds, typeof(DataTable));
				}
				if (dt != null)
				{
					if (index < dt.Rows.Count)
					{
						text=dt.Rows[index][field].ToString();
						SizeF sf;
						if (wordWrap)
						{
							sf=gr.MeasureString(text, ctrl.Font, Width-20);
						}
						else
						{
							sf=gr.MeasureString(text, ctrl.Font);
						}
						height=(int)sf.Height+1;
					}
					else
					{
						text=String.Empty;
					}
				}
			}
			catch(Exception e)
			{
				Trace.WriteLine(e.Message);
			}
		}

		protected string GetText(Control ctrl, int index)
		{
			string text=ctrl.Text;
			try
			{
				string dataSourceName=StringHelpers.LeftOf(text, ';').Trim();
				string field=StringHelpers.RightOf(text, '=').Trim();
				Parser parser=Parser.CurrentInstance;
				object ds=parser.GetReference(dataSourceName);
				DataTable dt=new DataTable();
				TypeConverter targetTypeConverter=TypeDescriptor.GetConverter(ds.GetType());
				Type t=dt.GetType();
				if (targetTypeConverter.CanConvertTo(t))
				{
					dt=(DataTable)targetTypeConverter.ConvertTo(ds, t);
				}
				text=(string)dt.Rows[index][field];
			}
			catch(Exception e)
			{
				Trace.WriteLine(e.Message);
			}
			return text;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions