ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Strongly Typed Databinding

This is an extension that I use for doing strongly typed databinding to controls in a winforms project. I dislike using strings to databind because they do not generate compiler errors when the bound object changes. This extension allows you to, instead of using a string, use an expression to bind to for both the control property and the object property.

Source

///<seealso cref="http://stackoverflow.com/questions/3444294/strong-typed-windows-forms-databinding"/>
	public static class ControlExtensions {

		/// <summary>Databinding with strongly typed object names</summary>
		/// <param name="control">The Control you are binding to</param>
		/// <param name="controlProperty">The property on the control you are binding to</param>
		/// <param name="dataSource">The object you are binding to</param>
		/// <param name="dataSourceProperty">The property on the object you are binding to</param>
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty));
		}
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled = false)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled);
		}
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode);
		}
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue);
		}
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString);
		}
		public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString, IFormatProvider formatInfo)
		where TControl :Control {
			return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString, formatInfo);
		}

		public static class PropertyName {
			public static string For<T>(Expression<Func<T, object>> property) {
				var member = property.Body as MemberExpression;
				if(null == member) {
					var unary = property.Body as UnaryExpression;
					if(null != unary) member = unary.Operand as MemberExpression;
				}
				return null != member ? member.Member.Name : string.Empty;
			}
		}

	}

Example

//txtCLSId is a TextBox
//_TaskListItem is an object of type ProjectServicesTaskList
//ProjectServicesTaskList contains a property (int) CLSHeaderID

txtCLSId.Bind(c => c.Text, _TaskListItem, ProjectServicesTaskList p) => p.CLSHeaderId);