1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
24 // Jackson Harper jackson@ximian.com
28 using System.ComponentModel;
30 namespace System.Windows.Forms {
32 [TypeConverter (typeof (ListBindingConverter))]
33 public class Binding {
35 private string property_name;
36 private object data_source;
37 private string data_member;
38 private BindingMemberInfo binding_member_info;
39 private Control control;
41 private BindingManagerBase manager;
42 private PropertyDescriptor prop_desc;
43 private PropertyDescriptor is_null_desc;
45 private EventDescriptor changed_event;
46 private EventHandler property_value_changed_handler;
47 private object event_current; // The manager.Current as far as the changed_event knows
50 private Type data_type;
52 #region Public Constructors
53 public Binding (string propertyName, object dataSource, string dataMember)
55 property_name = propertyName;
56 data_source = dataSource;
57 data_member = dataMember;
58 binding_member_info = new BindingMemberInfo (dataMember);
60 #endregion // Public Constructors
62 #region Public Instance Properties
63 public BindingManagerBase BindingManagerBase {
69 public BindingMemberInfo BindingMemberInfo {
71 return binding_member_info;
76 public Control Control {
82 public object DataSource {
88 public bool IsBinding {
90 if (control == null || !control.Created)
92 if (manager == null || manager.IsSuspended)
99 public string PropertyName {
101 return property_name;
104 #endregion // Public Instance Properties
106 #region Protected Instance Methods
107 protected virtual void OnFormat (ConvertEventArgs cevent)
110 Format (this, cevent);
113 protected virtual void OnParse (ConvertEventArgs cevent)
116 Parse (this, cevent);
118 #endregion // Protected Instance Methods
121 internal void SetControl (Control control)
123 if (control == this.control)
126 prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
127 data_type = prop_desc.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
129 if (prop_desc == null)
130 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
131 if (prop_desc.IsReadOnly)
132 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
134 control.Validating += new CancelEventHandler (ControlValidatingHandler);
136 this.control = control;
139 internal void Check (BindingContext binding_context)
144 manager = control.BindingContext [data_source, property_name];
145 manager.AddBinding (this);
147 WirePropertyValueChangedEvent ();
149 is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
154 internal void PushData ()
156 data = prop_desc.GetValue (control);
157 data = FormatData (data);
158 SetPropertyValue (data);
161 internal void PullData ()
163 if (is_null_desc != null) {
164 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
166 data = Convert.DBNull;
171 if (data_member != null) {
172 PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
173 object pulled = pd.GetValue (manager.Current);
174 data = ParseData (pulled, pd.PropertyType);
176 object pulled = manager.Current;
177 data = ParseData (pulled, pulled.GetType ());
180 data = FormatData (data);
181 SetControlValue (data);
184 internal void UpdateIsBinding ()
189 private void SetControlValue (object data)
191 prop_desc.SetValue (control, data);
194 private void SetPropertyValue (object data)
196 PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
199 pd.SetValue (manager.Current, data);
202 private void CurrentChangedHandler ()
204 if (changed_event != null) {
205 changed_event.RemoveEventHandler (event_current, property_value_changed_handler);
206 WirePropertyValueChangedEvent ();
210 private void WirePropertyValueChangedEvent ()
212 EventDescriptor changed_event = TypeDescriptor.GetEvents (manager.Current).Find (property_name + "Changed", false);
213 if (changed_event == null)
215 property_value_changed_handler = new EventHandler (PropertyValueChanged);
216 changed_event.AddEventHandler (manager.Current, property_value_changed_handler);
218 event_current = manager.Current;
221 private void PropertyValueChanged (object sender, EventArgs e)
226 private void ControlValidatingHandler (object sender, CancelEventArgs e)
231 private object ParseData (object data, Type data_type)
233 ConvertEventArgs e = new ConvertEventArgs (data, data_type);
236 if (data_type.IsAssignableFrom (e.Value.GetType ()))
238 if (e.Value == Convert.DBNull)
241 return ConvertData (e.Value, data_type);
244 private object FormatData (object data)
246 if (data_type == typeof (object))
249 ConvertEventArgs e = new ConvertEventArgs (data, data_type);
252 if (data_type.IsAssignableFrom (e.Value.GetType ()))
255 return ConvertData (data, data_type);
258 private object ConvertData (object data, Type data_type)
260 TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
261 if (converter != null && converter.CanConvertTo (data_type))
262 return converter.ConvertTo (data, data_type);
264 if (data is IConvertible) {
265 object res = Convert.ChangeType (data, data_type);
266 if (data_type.IsAssignableFrom (res.GetType ()))
274 public event ConvertEventHandler Format;
275 public event ConvertEventHandler Parse;