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;
46 private EventDescriptor changed_event;
47 private EventHandler property_value_changed_handler;
48 private object event_current; // The manager.Current as far as the changed_event knows
50 #region Public Constructors
51 public Binding (string propertyName, object dataSource, string dataMember)
53 property_name = propertyName;
54 data_source = dataSource;
55 data_member = dataMember;
56 binding_member_info = new BindingMemberInfo (dataMember);
58 #endregion // Public Constructors
60 #region Public Instance Properties
61 public BindingManagerBase BindingManagerBase {
67 public BindingMemberInfo BindingMemberInfo {
69 return binding_member_info;
74 public Control Control {
80 public object DataSource {
87 public bool IsBinding {
94 public string PropertyName {
99 #endregion // Public Instance Properties
101 #region Protected Instance Methods
102 protected virtual void OnFormat (ConvertEventArgs cevent)
105 Format (this, cevent);
108 protected virtual void OnParse (ConvertEventArgs cevent)
111 Parse (this, cevent);
113 #endregion // Protected Instance Methods
116 internal void SetControl (Control control)
118 if (control == this.control)
121 prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
123 if (prop_desc == null)
124 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
125 if (prop_desc.IsReadOnly)
126 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
128 control.Validating += new CancelEventHandler (ControlValidatingHandler);
130 this.control = control;
133 internal void Check (BindingContext binding_context)
138 manager = control.BindingContext [data_source, property_name];
139 manager.AddBinding (this);
141 WirePropertyValueChangedEvent ();
143 is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
149 internal void PushData ()
151 prop_desc.SetValue (control, data);
154 internal void PullData ()
156 if (is_null_desc != null) {
157 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
159 data = Convert.DBNull;
164 PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
165 data = pd.GetValue (manager.Current);
168 internal void UpdateIsBinding ()
173 private void CurrentChangedHandler ()
175 if (changed_event != null) {
176 changed_event.RemoveEventHandler (event_current, property_value_changed_handler);
177 WirePropertyValueChangedEvent ();
181 private void WirePropertyValueChangedEvent ()
183 EventDescriptor changed_event = TypeDescriptor.GetEvents (manager.Current).Find (property_name + "Changed", false);
184 if (changed_event == null)
186 property_value_changed_handler = new EventHandler (PropertyValueChanged);
187 changed_event.AddEventHandler (manager.Current, property_value_changed_handler);
189 event_current = manager.Current;
192 private void PropertyValueChanged (object sender, EventArgs e)
198 private void ControlValidatingHandler (object sender, CancelEventArgs e)
204 public event ConvertEventHandler Format;
205 public event ConvertEventHandler Parse;