Author: jackson
Date: 2005-03-04 14:22:48 -0500 (Fri, 04 Mar 2005)
New Revision: 41461

Modified:
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Binding.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
Log:
        * Binding.cs: Implement data type parsing and converting on pulled
        data. TODO: Are there more ways the data can be converted? 



Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Binding.cs
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Binding.cs       
2005-03-04 19:04:57 UTC (rev 41460)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Binding.cs       
2005-03-04 19:22:48 UTC (rev 41461)
@@ -41,12 +41,14 @@
                private BindingManagerBase manager;
                private PropertyDescriptor prop_desc;
                private PropertyDescriptor is_null_desc;
-               private object data;
 
                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)
                {
@@ -119,7 +121,8 @@
                                return;
 
                        prop_desc = TypeDescriptor.GetProperties (control).Find 
(property_name, false);
-
+                       data_type = prop_desc.PropertyType; // Getting the 
PropertyType is kinda slow and it should never change, so it is cached
+                       
                        if (prop_desc == null)
                                throw new ArgumentException (String.Concat 
("Cannot bind to property '", property_name, "' on target control."));
                        if (prop_desc.IsReadOnly)
@@ -162,7 +165,8 @@
                        }
 
                        PropertyDescriptor pd = TypeDescriptor.GetProperties 
(manager.Current).Find (data_member, true);
-                       data = pd.GetValue (manager.Current);
+                       object pulled = pd.GetValue (manager.Current);
+                       data = ParseData (pulled);
                }
 
                internal void UpdateIsBinding ()
@@ -200,6 +204,30 @@
                        PullData ();
                }
 
+                // TODO: Are there more ways the data can be converted?
+               private object ParseData (object data)
+               {
+                       ConvertEventArgs e = new ConvertEventArgs (data, 
data_type);
+
+                       OnParse (e);
+                       if (e.Value.GetType ().IsSubclassOf (data_type))
+                               return e.Value;
+                       if (e.Value == Convert.DBNull)
+                               return e.Value;
+
+                       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 (res.GetType ().IsSubclassOf (data_type))
+                                       return res;
+                       }
+
+                       return null;
+               }
+
                #region Events
                public event ConvertEventHandler Format;
                public event ConvertEventHandler Parse;

Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2005-03-04 19:04:57 UTC (rev 41460)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2005-03-04 19:22:48 UTC (rev 41461)
@@ -1,5 +1,10 @@
 2005-03-04  Jackson Harper  <[EMAIL PROTECTED]>
 
+       * Binding.cs: Implement data type parsing and converting on pulled
+       data. TODO: Are there more ways the data can be converted? 
+
+2005-03-04  Jackson Harper  <[EMAIL PROTECTED]>
+
        * Binding.cs: Support <Property>IsNull checks. Also bind to the
        controls Validating method so we can repull the data when the
        control loses focus.

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to