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 // Jackson Harper (jackson@ximian.com)
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Reflection;
32 namespace System.Windows.Forms {
33 [DefaultEvent("CollectionChanged")]
34 [Editor("System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing, typeof(System.Drawing.Design.UITypeEditor))]
35 [TypeConverter("System.Windows.Forms.Design.ControlBindingsConverter, " + Consts.AssemblySystem_Design)]
36 public class ControlBindingsCollection : BindingsCollection {
38 private Control control;
40 private IBindableComponent bindable_component;
41 private DataSourceUpdateMode default_datasource_update_mode;
46 internal ControlBindingsCollection (Control control) {
47 this.control = control;
49 bindable_component = control as IBindableComponent;
50 default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
55 public ControlBindingsCollection (IBindableComponent control)
57 bindable_component = control;
58 control = control as Control;
59 default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
62 #endregion // Constructors
64 #region Public Instance Properties
65 public Control Control {
71 public Binding this[string propertyName] {
73 foreach (Binding b in base.List) {
74 if (b.PropertyName == propertyName) {
83 public IBindableComponent BindableComponent {
85 return bindable_component;
89 public DataSourceUpdateMode DefaultDataSourceUpdateMode {
91 return default_datasource_update_mode;
94 default_datasource_update_mode = value;
98 #endregion // Public Instance Properties
100 #region Public Instance Methods
101 public new void Add (Binding binding)
104 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, binding));
107 public Binding Add (string propertyName, object dataSource, string dataMember)
109 if (dataSource == null)
110 throw new ArgumentNullException ("dataSource");
112 Binding res = new Binding (propertyName, dataSource, dataMember);
114 res.DataSourceUpdateMode = default_datasource_update_mode;
121 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled)
123 return Add (propertyName, dataSource, dataMember, formattingEnabled, default_datasource_update_mode, null, String.Empty, null);
126 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode)
128 return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, null, String.Empty, null);
131 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
134 return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, String.Empty, null);
137 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
138 object nullValue, string formatString)
140 return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, formatString, null);
143 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
144 object nullValue, string formatString, IFormatProvider formatInfo)
146 if (dataSource == null)
147 throw new ArgumentNullException ("dataSource");
149 Binding res = new Binding (propertyName, dataSource, dataMember);
150 res.FormattingEnabled = formattingEnabled;
151 res.DataSourceUpdateMode = updateMode;
152 res.NullValue = nullValue;
153 res.FormatString = formatString;
154 res.FormatInfo = formatInfo;
161 public new void Clear() {
165 public new void Remove (Binding binding) {
167 throw new NullReferenceException("The binding is null");
169 base.Remove(binding);
172 public new void RemoveAt(int index) {
173 if (index < 0 || index >= base.List.Count)
174 throw new ArgumentOutOfRangeException("index");
176 base.RemoveAt(index);
178 #endregion // Public Instance Methods
180 #region Protected Instance Methods
181 protected override void AddCore (Binding dataBinding)
183 if (dataBinding == null)
184 throw new ArgumentNullException ("dataBinding");
187 if (dataBinding.Control != null && dataBinding.BindableComponent != bindable_component)
189 if (dataBinding.Control != null && dataBinding.Control != control)
191 throw new ArgumentException ("dataBinding belongs to another BindingsCollection");
193 for (int i = 0; i < Count; i++) {
194 Binding bnd = this [i];
195 if (bnd == null || bnd.PropertyName.Length == 0 || dataBinding.PropertyName.Length == 0) {
199 if (String.Compare (bnd.PropertyName, dataBinding.PropertyName, true) == 0) {
200 throw new ArgumentException ("The binding is already in the collection");
205 dataBinding.SetControl (bindable_component);
207 dataBinding.SetControl (control);
209 dataBinding.Check ();
210 base.AddCore (dataBinding);
213 protected override void ClearCore() {
217 protected override void RemoveCore (Binding dataBinding) {
218 if (dataBinding == null)
219 throw new ArgumentNullException ("dataBinding");
221 base.RemoveCore (dataBinding);
223 #endregion // Protected Instance Methods