download Binding.cs
Language: C#
Copyright: (c) 2004-2005 Novell, Inc.
LOC: 200
Project Info
mcs
Server: Mono
Type: svn
...Forms\System.Windows.Forms\
   AccessibleEvents.cs
   AccessibleNavigation.cs
   AccessibleObject.cs
   AccessibleRole.cs
   AccessibleSelection.cs
   AccessibleStates.cs
   AmbientProperties.cs
   AnchorStyles.cs
   Appearance.cs
   Application.cs
   ApplicationContext.cs
   ArrangeDirection.cs
   ArrangeStartingPosition.cs
   ArrowDirection.cs
   AsyncMethodData.cs
   AsyncMethodResult.cs
   AutoCompleteMode.cs
   AutoCompleteSource.cs
   AutoScaleMode.cs
   AutoSizeMode.cs
   AutoValidate.cs
   AxHost.cs
   BaseCollection.cs
   BatteryChargeStatus.cs
   Binding.cs
   BindingCompleteContext.cs
   ...ingCompleteEventArgs.cs
   ...CompleteEventHandler.cs
   BindingCompleteState.cs
   BindingContext.cs
   BindingManagerBase.cs
   ...erDataErrorEventArgs.cs
   ...ataErrorEventHandler.cs
   BindingMemberInfo.cs
   BindingsCollection.cs
   BootMode.cs
   Border3DSide.cs
   Border3DStyle.cs
   BorderStyle.cs
   BoundsSpecified.cs
   Button.cs
   ButtonBase.cs
   ButtonBorderStyle.cs
   ButtonRenderer.cs
   ButtonState.cs
   ...irtualItemsEventArgs.cs
   ...ualItemsEventHandler.cs
   CaptionButton.cs
   CategoryGridEntry.cs
   CharacterCasing.cs
   CheckBox.cs
   CheckBoxRenderer.cs
   CheckedListBox.cs
   CheckState.cs
   Clipboard.cs
   CloseReason.cs
   ColorDepth.cs
   ColorDialog.cs
   ColumnClickEventArgs.cs
   ColumnClickEventHandler.cs
   ColumnHeader.cs
   ...eaderAutoResizeStyle.cs
   ColumnHeaderStyle.cs
   ...mnReorderedEventArgs.cs
   ...eorderedEventHandler.cs
   ColumnStyle.cs
   ...idthChangedEventArgs.cs
   ...hChangedEventHandler.cs
   ...dthChangingEventArgs.cs
   ...ChangingEventHandler.cs
   ComboBox.cs
   ComboBoxRenderer.cs
   ComboBoxStyle.cs
   CommonDialog.cs
   ComponentModel.cs
   ContainerControl.cs
   ...entsResizedEventArgs.cs
   ...sResizedEventHandler.cs
   ContextMenu.cs
   Control.cs
   ...olBindingsCollection.cs
   ControlEventArgs.cs
   ControlEventHandler.cs
   ControlPaint.cs
   ControlStyles.cs
   ControlUpdateMode.cs
   ConvertEventArgs.cs
   ConvertEventHandler.cs
   CreateParams.cs
   CurrencyManager.cs
   Cursor.cs
   CursorConverter.cs
   Cursors.cs
   DataFormats.cs
   DataGrid.cs
   DataGridBoolColumn.cs
   DataGridCell.cs
   DataGridColumnStyle.cs
   DataGridLineStyle.cs
   ...ParentRowsLabelStyle.cs
   ...mnWidthTypeConverter.cs
   DataGridTableStyle.cs
   DataGridTextBox.cs
   DataGridTextBoxColumn.cs
   DataGridView.cs
   ...wAdvancedBorderStyle.cs
   ...ancedCellBorderStyle.cs
   ...ewAutoSizeColumnMode.cs
   ...eColumnModeEventArgs.cs
   ...lumnModeEventHandler.cs
   ...wAutoSizeColumnsMode.cs
   ...ColumnsModeEventArgs.cs
   ...umnsModeEventHandler.cs
   ...utoSizeModeEventArgs.cs
   ...SizeModeEventHandler.cs
   ...dViewAutoSizeRowMode.cs
   ...ViewAutoSizeRowsMode.cs
   DataGridViewBand.cs
   ...ingCompleteEventArgs.cs
   ...CompleteEventHandler.cs
   DataGridViewButtonCell.cs
   ...GridViewButtonColumn.cs
   DataGridViewCell.cs
   ...dViewCellBorderStyle.cs
   ...wCellCancelEventArgs.cs
   ...llCancelEventHandler.cs
   ...idViewCellCollection.cs
   ...StripNeededEventArgs.cs
   ...ipNeededEventHandler.cs
   ...rTextNeededEventArgs.cs
   ...xtNeededEventHandler.cs
   ...ridViewCellEventArgs.cs
   ...ViewCellEventHandler.cs
   ...lFormattingEventArgs.cs
   ...rmattingEventHandler.cs
   ...ewCellMouseEventArgs.cs
   ...ellMouseEventHandler.cs
   ...ellPaintingEventArgs.cs
   ...PaintingEventHandler.cs
   ...CellParsingEventArgs.cs
   ...lParsingEventHandler.cs
   ...tateChangedEventArgs.cs
   ...eChangedEventHandler.cs
   DataGridViewCellStyle.cs
   ...tentChangedEventArgs.cs
   ...tChangedEventHandler.cs
   ...ewCellStyleConverter.cs
   ...dViewCellStyleScopes.cs
   ...pTextNeededEventArgs.cs
   ...xtNeededEventHandler.cs
   ...lValidatingEventArgs.cs
   ...lidatingEventHandler.cs
   ...ewCellValueEventArgs.cs
   ...ellValueEventHandler.cs
   ...GridViewCheckBoxCell.cs
   ...idViewCheckBoxColumn.cs
   ...iewClipboardCopyMode.cs
   DataGridViewColumn.cs
   ...ViewColumnCollection.cs
   ...TimeVisibleAttribute.cs
   ...DoubleClickEventArgs.cs
   ...bleClickEventHandler.cs
   ...dViewColumnEventArgs.cs
   ...ewColumnEventHandler.cs
   ...ViewColumnHeaderCell.cs
   ...eadersHeightSizeMode.cs
   ...idViewColumnSortMode.cs
   ...tateChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...GridViewComboBoxCell.cs
   ...idViewComboBoxColumn.cs
   ...ComboBoxDisplayStyle.cs
   ...mboBoxEditingControl.cs
   ...ViewContentAlignment.cs
   ...iewDataErrorContexts.cs
   ...ewDataErrorEventArgs.cs
   ...ataErrorEventHandler.cs
   ...trolShowingEventArgs.cs
   ...lShowingEventHandler.cs
   DataGridViewEditMode.cs
   DataGridViewElement.cs
   ...ridViewElementStates.cs
   ...iewHeaderBorderStyle.cs
   DataGridViewHeaderCell.cs
   DataGridViewHitTestType.cs
   DataGridViewImageCell.cs
   ...dViewImageCellLayout.cs
   DataGridViewImageColumn.cs
   DataGridViewPaintParts.cs
   DataGridViewRow.cs
   ...ewRowCancelEventArgs.cs
   ...owCancelEventHandler.cs
   ...ridViewRowCollection.cs
   ...StripNeededEventArgs.cs
   ...ipNeededEventHandler.cs
   ...DoubleClickEventArgs.cs
   ...bleClickEventHandler.cs
   ...rTextNeededEventArgs.cs
   ...xtNeededEventHandler.cs
   ...GridViewRowEventArgs.cs
   ...dViewRowEventHandler.cs
   ...ridViewRowHeaderCell.cs
   ...HeadersWidthSizeMode.cs
   ...tInfoNeededEventArgs.cs
   ...foNeededEventHandler.cs
   ...tInfoPushedEventArgs.cs
   ...foPushedEventHandler.cs
   ...owPostPaintEventArgs.cs
   ...ostPaintEventHandler.cs
   ...RowPrePaintEventArgs.cs
   ...PrePaintEventHandler.cs
   ...ewRowsAddedEventArgs.cs
   ...owsAddedEventHandler.cs
   ...RowsDeletedEventArgs.cs
   ...RowsRemovedEventArgs.cs
   ...sRemovedEventHandler.cs
   ...tateChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...lectedCellCollection.cs
   ...ctedColumnCollection.cs
   ...electedRowCollection.cs
   ...ridViewSelectionMode.cs
   ...SortCompareEventArgs.cs
   ...tCompareEventHandler.cs
   DataGridViewTextBoxCell.cs
   ...ridViewTextBoxColumn.cs
   ...extBoxEditingControl.cs
   ...iewTopLeftHeaderCell.cs
   DataGridViewTriState.cs
   DataObject.cs
   DataSourceUpdateMode.cs
   DateBoldEventArgs.cs
   DateBoldEventHandler.cs
   DateRangeEventArgs.cs
   DateRangeEventHandler.cs
   DateTimePicker.cs
   DateTimePickerFormat.cs
   Day.cs
   DialogResult.cs
   DockingBehavior.cs
   DockStyle.cs
   DomainUpDown.cs
   DragAction.cs
   DragDropEffects.cs
   DragEventArgs.cs
   DragEventHandler.cs
   DrawItemEventArgs.cs
   DrawItemEventHandler.cs
   DrawItemState.cs
   DrawMode.cs
   ErrorBlinkStyle.cs
   ErrorIconAlignment.cs
   ErrorProvider.cs
   FeatureSupport.cs
   FileDialog.cs
   FixedPanel.cs
   FixedSizeTextBox.cs
   FlatStyle.cs
   FlowDirection.cs
   FolderBrowserDialog.cs
   FontDialog.cs
   Form.cs
   FormBorderStyle.cs
   FormClosedEventArgs.cs
   FormClosedEventHandler.cs
   FormClosingEventArgs.cs
   FormClosingEventHandler.cs
   FormStartPosition.cs
   FormWindowState.cs
   FrameStyle.cs
   GetChildAtPointSkip.cs
   GiveFeedbackEventArgs.cs
   ...FeedbackEventHandler.cs
   ...lumnStylesCollection.cs
   GridEntry.cs
   GridItem.cs
   GridItemCollection.cs
   GridItemType.cs
   GridTablesFactory.cs
   ...ableStylesCollection.cs
   GroupBox.cs
   GroupBoxRenderer.cs
   HandledMouseEventArgs.cs
   Help.cs
   HelpEventArgs.cs
   HelpEventHandler.cs
   HelpNavigator.cs
   HelpProvider.cs
   HorizontalAlignment.cs
   HScrollBar.cs
   ...InsertionOrientation.cs
   Hwnd.cs
   IButtonControl.cs
   ICommandExecutor.cs
   ...ponentEditorPageSite.cs
   IContainerControl.cs
   ...gNotificationService.cs
   IDataGridEditingService.cs
   ...aGridViewEditingCell.cs
   ...idViewEditingControl.cs
   IDataObject.cs
   IDropTarget.cs
   IFeatureSupport.cs
   IFileReaderService.cs
   ImageIndexConverter.cs
   ImageLayout.cs
   ImageList.cs
   ImageListConverter.cs
   ImageListStreamer.cs
   ImeMode.cs
   IMessageFilter.cs
   ImplicitHScrollBar.cs
   ImplicitVScrollBar.cs
   InputLanguage.cs
   ...uageChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...ageChangingEventArgs.cs
   ...ChangingEventHandler.cs
   InputLanguageCollection.cs
   InsertKeyMode.cs
   InternalWindowManager.cs
   InvalidateEventArgs.cs
   InvalidateEventHandler.cs
   IRootGridEntry.cs
   ItemActivation.cs
   ItemBoundsPortion.cs
   ItemChangedEventArgs.cs
   ItemChangedEventHandler.cs
   ItemCheckedEventArgs.cs
   ItemCheckedEventHandler.cs
   ItemCheckEventArgs.cs
   ItemCheckEventHandler.cs
   ItemDragEventArgs.cs
   ItemDragEventHandler.cs
   IWin32Window.cs
   IWindowTarget.cs
   KeyboardLayouts.cs
   KeyEventArgs.cs
   KeyEventHandler.cs
   KeyPressEventArgs.cs
   KeyPressEventHandler.cs
   Keys.cs
   KeysConverter.cs
   Label.cs
   LabelEditEventArgs.cs
   LabelEditEventHandler.cs
   LabelEditTextBox.cs
   LayoutEngine.cs
   LayoutEventArgs.cs
   LayoutEventHandler.cs
   LayoutSettings.cs
   LeftRightAlignment.cs
   LibSupport.cs
   LinkArea.cs
   LinkBehavior.cs
   LinkClickedEventArgs.cs
   LinkClickedEventHandler.cs
   LinkLabel.cs
   ...LinkClickedEventArgs.cs
   ...kClickedEventHandler.cs
   LinkState.cs
   ListBindingConverter.cs
   ListBox.cs
   ListControl.cs
   ...trolConvertEventArgs.cs
   ...lConvertEventHandler.cs
   ListView.cs
   ListViewAlignment.cs
   ...ViewHitTestLocations.cs
   ListViewItem.cs
   ListViewItemConverter.cs
   ...mMouseHoverEventArgs.cs
   ...useHoverEventHandler.cs
   ...tionChangedEventArgs.cs
   ...nChangedEventHandler.cs
   ListViewItemStates.cs
   ...angeChangedEventArgs.cs
   ...eChangedEventHandler.cs
   MainMenu.cs
   MaskFormat.cs
   ...putRejectedEventArgs.cs
   ...RejectedEventHandler.cs
   MdiChildContext.cs
   MdiClient.cs
   MdiLayout.cs
   MdiWindowManager.cs
   MeasureItemEventArgs.cs
   MeasureItemEventHandler.cs
   Menu.cs
   MenuAPI.cs
   MenuGlyph.cs
   MenuItem.cs
   MenuMerge.cs
   MenuStrip.cs
   MergeAction.cs
   Message.cs
   MessageBox.cs
   MessageBoxButtons.cs
   MessageBoxDefaultButton.cs
   MessageBoxIcon.cs
   MessageBoxOptions.cs
   MethodInvoker.cs
   Mime.cs
   MimeIcon.cs
   MonthCalendar.cs
   MouseButtons.cs
   MouseEventArgs.cs
   MouseEventHandler.cs
   MWFCategoryAttribute.cs
   MWFDescriptionAttribute.cs
   NativeWindow.cs
   NavigateEventArgs.cs
   NavigateEventHandler.cs
   NodeLabelEditEventArgs.cs
   ...abelEditEventHandler.cs
   NotifyIcon.cs
   NumericUpDown.cs
   OpacityConverter.cs
   OpenFileDialog.cs
   OpenTreeNodeEnumerator.cs
   Orientation.cs
   OSFeature.cs
   OSXStructs.cs
   OwnerDrawPropertyBag.cs
   Padding.cs
   PageSetupDialog.cs
   PaintEventArgs.cs
   PaintEventHandler.cs
   Panel.cs
   PictureBox.cs
   PictureBoxSizeMode.cs
   PopupEventArgs.cs
   PopupEventHandler.cs
   PowerLineStatus.cs
   PowerState.cs
   PreProcessControlState.cs
   PreviewKeyDownEventArgs.cs
   ...wKeyDownEventHandler.cs
   ...llerWithStatusDialog.cs
   PrintDialog.cs
   PrintPreviewControl.cs
   PrintPreviewDialog.cs
   ProfessionalColors.cs
   ProfessionalColorTable.cs
   ProgressBar.cs
   ProgressBarRenderer.cs
   ProgressBarStyle.cs
   PropertiesTab.cs
   PropertyGrid.cs
   PropertyGridCommands.cs
   PropertyGridEntry.cs
   PropertyGridTextBox.cs
   PropertyGridView.cs
   PropertyManager.cs
   PropertySort.cs
   ...yTabChangedEventArgs.cs
   ...bChangedEventHandler.cs
   ...alueChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...ibilityHelpEventArgs.cs
   ...lityHelpEventHandler.cs
   ...ontinueDragEventArgs.cs
   ...inueDragEventHandler.cs
   QuestionEventArgs.cs
   QuestionEventHandler.cs
   RadioButton.cs
   RadioButtonRenderer.cs
   RelatedCurrencyManager.cs
   RelatedPropertyManager.cs
   ...VirtualItemEventArgs.cs
   ...tualItemEventHandler.cs
   RichTextBox.cs
   RichTextBoxFinds.cs
   ...xtBoxLanguageOptions.cs
   RichTextBoxScrollBars.cs
   ...oxSelectionAttribute.cs
   ...extBoxSelectionTypes.cs
   RichTextBoxStreamType.cs
   ...tBoxWordPunctuations.cs
   RightToLeft.cs
   RowStyle.cs
   SaveFileDialog.cs
   Screen.cs
   ScreenOrientation.cs
   ScrollableControl.cs
   ScrollBar.cs
   ScrollBarRenderer.cs
   ScrollBars.cs
   ScrollButton.cs
   ScrollEventArgs.cs
   ScrollEventHandler.cs
   ScrollEventType.cs
   ScrollOrientation.cs
   SearchDirectionHint.cs
   ...VirtualItemEventArgs.cs
   ...tualItemEventHandler.cs
   SecurityIDType.cs
   ...ItemChangedEventArgs.cs
   ...mChangedEventHandler.cs
   SelectionMode.cs
   SelectionRange.cs
   SelectionRangeConverter.cs
   SendKeys.cs
   Shortcut.cs
   SizeGrip.cs
   SizeGripStyle.cs
   SizeType.cs
   SortOrder.cs
   Splitter.cs
   SplitterCancelEventArgs.cs
   ...erCancelEventHandler.cs
   SplitterEventArgs.cs
   SplitterEventHandler.cs
   StatusBar.cs
   ...BarDrawItemEventArgs.cs
   ...DrawItemEventHandler.cs
   StatusBarPanel.cs
   StatusBarPanelAutoSize.cs
   ...sBarPanelBorderStyle.cs
   ...rPanelClickEventArgs.cs
   ...nelClickEventHandler.cs
   StatusBarPanelStyle.cs
   StructFormat.cs
   SWF.csproj.user
   SystemInformation.cs
   SystemParameter.cs
   TabAlignment.cs
   TabAppearance.cs
   TabControl.cs
   TabControlAction.cs
   ...ntrolCancelEventArgs.cs
   ...olCancelEventHandler.cs
   TabControlEventArgs.cs
   TabControlEventHandler.cs
   TabDrawMode.cs
   TableLayout.cs
   ...utCellPaintEventArgs.cs
   ...ellPaintEventHandler.cs
   TableLayoutPanel.cs
   ...PanelCellBorderStyle.cs
   ...LayoutPanelGrowStyle.cs
   TableLayoutSettings.cs
   TabPage.cs
   TabRenderer.cs
   TabSizeMode.cs
   TextBox.cs
   TextBoxBase.cs
   TextBoxRenderer.cs
   TextControl.cs
   TextDataFormat.cs
   TextFormatFlags.cs
   TextImageRelation.cs
   TextRenderer.cs
   Theme.cs
   ThemeClearlooks.cs
   ThemeEngine.cs
   ThemeGtk.cs
   ThemeNice.cs
   ThemeWin32Classic.cs
   ThreadExceptionDialog.cs
   TickStyle.cs
   Timer.cs
   ToolBar.cs
   ToolBarAppearance.cs
   ToolBarButton.cs
   ...ButtonClickEventArgs.cs
   ...tonClickEventHandler.cs
   ToolBarButtonStyle.cs
   ToolBarTextAlign.cs
   ToolStrip.cs
   ...pDownClosedEventArgs.cs
   ...wnClosedEventHandler.cs
   ...pDropDownCloseReason.cs
   ...DownClosingEventArgs.cs
   ...nClosingEventHandler.cs
   ...ripDropDownDirection.cs
   ToolStripDropDownItem.cs
   ...tripGripDisplayStyle.cs
   ToolStripGripStyle.cs
   ToolStripItem.cs
   ToolStripItemAlignment.cs
   ...tripItemDisplayStyle.cs
   ...tripItemImageScaling.cs
   ToolStripItemOverflow.cs
   ToolStripItemPlacement.cs
   ToolStripLayoutStyle.cs
   ...ripManagerRenderMode.cs
   ToolStripMenuItem.cs
   ToolStripRenderMode.cs
   ...atusLabelBorderSides.cs
   ToolStripTextDirection.cs
   ToolTip.cs
   ToolTipIcon.cs
   TrackBar.cs
   TrackBarRenderer.cs
   TreeNode.cs
   TreeNodeCollection.cs
   TreeNodeConverter.cs
   ...eMouseClickEventArgs.cs
   ...useClickEventHandler.cs
   ...eMouseHoverEventArgs.cs
   ...useHoverEventHandler.cs
   TreeNodeStates.cs
   TreeView.cs
   TreeViewAction.cs
   TreeViewCancelEventArgs.cs
   ...ewCancelEventHandler.cs
   TreeViewDrawMode.cs
   TreeViewEventArgs.cs
   TreeViewEventHandler.cs
   ...ViewHitTestLocations.cs
   ...wImageIndexConverter.cs
   TypeValidationEventArgs.cs
   ...lidationEventHandler.cs
   UICues.cs
   UICuesEventArgs.cs
   UICuesEventHandler.cs
   UnhandledExceptionMode.cs
   UpDownBase.cs
   UpDownEventArgs.cs
   UpDownEventHandler.cs
   UserControl.cs
   ValidationConstraints.cs
   View.cs
   VScrollBar.cs
   ...ntCompletedEventArgs.cs
   ...ompletedEventHandler.cs
   ...owserEncryptionLevel.cs
   ...erNavigatedEventArgs.cs
   ...avigatedEventHandler.cs
   ...rNavigatingEventArgs.cs
   ...vigatingEventHandler.cs
   ...ressChangedEventArgs.cs
   ...sChangedEventHandler.cs
   WebBrowserReadyState.cs
   WebBrowserRefreshOption.cs
   Win32DnD.cs
   X11DesktopColors.cs
   X11Dnd.cs
   X11Keyboard.cs
   X11Structs.cs
   XEventQueue.cs
   XplatUI.cs
   XplatUIDriver.cs
   XplatUIOSX.cs
   XplatUIStructs.cs
   XplatUIWin32.cs
   XplatUIX11.cs
   XplatUIX11GTK.cs

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2005 Novell, Inc.
//
// Authors:
//	Peter Bartok	pbartok@novell.com
//	Jackson Harper	jackson@ximian.com
//


using System.ComponentModel;

namespace System.Windows.Forms {

	[TypeConverter (typeof (ListBindingConverter))]
	public class Binding {

		private string property_name;
		private object data_source;
		private string data_member;

		private BindingMemberInfo binding_member_info;
		private Control control;

		private BindingManagerBase manager;
		private PropertyDescriptor control_property;
		private PropertyDescriptor is_null_desc;

		private EventDescriptor changed_event;
		private EventHandler property_value_changed_handler;
		private object event_current; // The manager.Current as far as the changed_event knows

		private object data;
		private Type data_type;

		#region Public Constructors
		public Binding (string propertyName, object dataSource, string dataMember)
		{
			property_name = propertyName;
			data_source = dataSource;
			data_member = dataMember;
			binding_member_info = new BindingMemberInfo (dataMember);
		}
		#endregion	// Public Constructors

		#region Public Instance Properties
		public BindingManagerBase BindingManagerBase {
			get {
				return manager;
			}
		}

		public BindingMemberInfo BindingMemberInfo {
			get {
				return binding_member_info;
			}
		}

		[DefaultValue (null)]
		public Control Control {
			get {
				return control;
			}
		}

		public object DataSource {
			get {
				return data_source;
			}
		}

		public bool IsBinding {
			get {
				if (control == null || !control.Created)
					return false;
				if (manager == null || manager.IsSuspended)
					return false;
				return true;
			}
		}

		[DefaultValue ("")]
		public string PropertyName {
			get {
				return property_name;
			}
		}
		#endregion	// Public Instance Properties

		#region Protected Instance Methods
		protected virtual void OnFormat (ConvertEventArgs cevent)
		{
			if (Format!=null)
				Format (this, cevent);
		}

		protected virtual void OnParse (ConvertEventArgs cevent)
		{
			if (Parse!=null)
				Parse (this, cevent);
		}
		#endregion	// Protected Instance Methods

		
		internal void SetControl (Control control)
		{
			if (control == this.control)
				return;

			control_property = TypeDescriptor.GetProperties (control).Find (property_name, true);			

			if (control_property == null)
				throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
			if (control_property.IsReadOnly)
				throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
				
			data_type = control_property.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
			control.Validating += new CancelEventHandler (ControlValidatingHandler);

			this.control = control;
			control.DataBindings.Add (this);
		}

		internal void Check (BindingContext binding_context)
		{
			if (control == null || control.BindingContext == null)
				return;

			manager = control.BindingContext [data_source, data_member];

			manager.AddBinding (this);
			manager.PositionChanged += new EventHandler (PositionChangedHandler);
			manager.CurrentChanged += new EventHandler (CurrentChangedHandler);

			is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);

			PushData ();
		}

		internal void PullData ()
		{
			if (IsBinding == false || manager.Current == null)
				return;

			data = control_property.GetValue (control);
			SetPropertyValue (data);
		}

		internal void PushData ()
		{
			if (manager == null || manager.IsSuspended || manager.Current == null)
				return;

			if (is_null_desc != null) {
				bool is_null = (bool) is_null_desc.GetValue (manager.Current);
				if (is_null) {
					data = Convert.DBNull;
					return;
				}
			}

			PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
			if (pd == null) {
				data = ParseData (manager.Current, manager.Current.GetType ());
			} else {
				data = ParseData (pd.GetValue (manager.Current), pd.PropertyType);
			}

			data = FormatData (data);
			SetControlValue (data);
		}

		internal void UpdateIsBinding ()
		{
			PushData ();
		}

		private void SetControlValue (object data)
		{
			control_property.SetValue (control, data);
		}

		private void SetPropertyValue (object data)
		{
			PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
			if (pd.IsReadOnly)
				return;
			data = ParseData (data, pd.PropertyType);
			pd.SetValue (manager.Current, data);
		}

		private void CurrentChangedHandler (object sender, EventArgs e)
		{
			PushData ();
		}

		private void ControlValidatingHandler (object sender, CancelEventArgs e)
		{
			object old_data = data;

			// If the data doesn't seem to be valid (it can't be converted,
			// is the wrong type, etc, we reset to the old data value.
			try {
				PullData ();
			} catch {
				data = old_data;
				SetControlValue (data);
			}
		}

		private void PositionChangedHandler (object sender, EventArgs e)
		{
			PushData ();
		}

		private object ParseData (object data, Type data_type)
		{
			ConvertEventArgs e = new ConvertEventArgs (data, data_type);

			OnParse (e);
			if (data_type.IsInstanceOfType (e.Value))
				return e.Value;
			if (e.Value == Convert.DBNull)
				return e.Value;

			return ConvertData (e.Value, data_type);
		}

		private object FormatData (object data)
		{
			if (data_type == typeof (object)) 
				return data;

			ConvertEventArgs e = new ConvertEventArgs (data, data_type);

			OnFormat (e);
			if (data_type.IsInstanceOfType (e.Value))
				return e.Value;

			return ConvertData (data, data_type);
		}

		private object ConvertData (object data, Type data_type)
		{
			TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
			if (converter != null && converter.CanConvertTo (data_type))
				return converter.ConvertTo (data, data_type);

			if (data is IConvertible) {
				object res = Convert.ChangeType (data, data_type);
				if (data_type.IsInstanceOfType (res))
					return res;
			}

			return null;
		}

		#region Events
		public event ConvertEventHandler Format;
		public event ConvertEventHandler Parse;
		#endregion	// Events
	}
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us