* BindingContext.cs:
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Binding.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Jackson Harper  jackson@ximian.com
25 //
26
27
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms {
31
32         [TypeConverter (typeof (ListBindingConverter))]
33         public class Binding {
34
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;
40
41                 private BindingManagerBase manager;
42                 private PropertyDescriptor prop_desc;
43                 private object data;
44
45                 #region Public Constructors
46                 public Binding (string propertyName, object dataSource, string dataMember)
47                 {
48                         property_name = propertyName;
49                         data_source = dataSource;
50                         data_member = dataMember;
51                         binding_member_info = new BindingMemberInfo (dataMember);
52                 }
53                 #endregion      // Public Constructors
54
55                 #region Public Instance Properties
56                 public BindingManagerBase BindingManagerBase {
57                         get {
58                                 return manager;
59                         }
60                 }
61
62                 public BindingMemberInfo BindingMemberInfo {
63                         get {
64                                 return binding_member_info;
65                         }
66                 }
67
68                 [DefaultValue (null)]
69                 public Control Control {
70                         get {
71                                 return control;
72                         }
73                 }
74
75                 public object DataSource {
76                         get {
77                                 return data_source;
78                         }
79                 }
80
81                 [MonoTODO]
82                 public bool IsBinding {
83                         get {
84                                 return false;
85                         }
86                 }
87
88                 [DefaultValue ("")]
89                 public string PropertyName {
90                         get {
91                                 return property_name;
92                         }
93                 }
94                 #endregion      // Public Instance Properties
95
96                 #region Protected Instance Methods
97                 protected virtual void OnFormat (ConvertEventArgs cevent)
98                 {
99                         if (Format!=null)
100                                 Format (this, cevent);
101                 }
102
103                 protected virtual void OnParse (ConvertEventArgs cevent)
104                 {
105                         if (Parse!=null)
106                                 Parse (this, cevent);
107                 }
108
109
110
111                 #endregion      // Protected Instance Methods
112
113                 
114                 internal void SetControl (Control control)
115                 {
116                         if (control == this.control)
117                                 return;
118
119                         prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
120
121                         if (prop_desc == null)
122                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
123                         if (prop_desc.IsReadOnly)
124                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
125                         this.control = control;
126                 }
127
128                 internal void Check (BindingContext binding_context)
129                 {
130                         if (control == null)
131                                 return;
132
133                         manager = control.BindingContext [data_source, property_name];
134                         manager.AddBinding (this);
135
136                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, false);
137                         prop_desc.SetValue (control, pd.GetValue (manager.Current));
138
139                         PullData ();
140                 }
141
142                 internal void PushData ()
143                 {
144                         prop_desc.SetValue (control, data);
145                 }
146
147                 internal void PullData ()
148                 {
149                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, false);
150                         data = pd.GetValue (manager.Current);
151                 }
152
153                 #region Events
154                 public event ConvertEventHandler Format;
155                 public event ConvertEventHandler Parse;
156                 #endregion      // Events
157         }
158 }