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;
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)
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;
- Console.WriteLine ("data source {0} member name: {1}",
- data_source, data_member);
manager = control.BindingContext [data_source, data_member];
manager.AddBinding (this);
return;
data = control_property.GetValue (control);
- data = ParseData (data, manager.Current.GetType());
SetPropertyValue (data);
}
internal void PushData ()
{
- if (IsBinding == false || manager.Current == null)
+ if (manager == null || manager.IsSuspended || manager.Current == null)
return;
if (is_null_desc != null) {
PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
if (pd == null) {
data = ParseData (manager.Current, manager.Current.GetType ());
- }
- else {
+ } else {
data = ParseData (pd.GetValue (manager.Current), pd.PropertyType);
}
private void SetPropertyValue (object data)
{
- PropertyDescriptor pd = TypeDescriptor.GetProperties (data_source).Find (binding_member_info.BindingField, true);
+ PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
if (pd.IsReadOnly)
return;
- pd.SetValue (data_source, data);
+ data = ParseData (data, pd.PropertyType);
+ pd.SetValue (manager.Current, data);
}
private void CurrentChangedHandler (object sender, EventArgs e)
private void ControlValidatingHandler (object sender, CancelEventArgs e)
{
- PullData ();
+ 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)