CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BindingSourceRefresh.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BindingSourceRefresh.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>BindingSourceRefresh contains functionality for refreshing the data bound to controls on Host as well as a mechinism for catching data</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.ComponentModel;
10using System.Collections.Generic;
11using System.Diagnostics;
12using System.Windows.Forms;
13
14// code from Bill McCarthy
15// http://msmvps.com/bill/archive/2005/10/05/69012.aspx
16// used with permission
17
18namespace Csla.Windows
19{
27 [DesignerCategory("")]
28 [ProvideProperty("ReadValuesOnChange", typeof(BindingSource))]
29 public class BindingSourceRefresh : Component, IExtenderProvider, ISupportInitialize
30 {
31 #region Fields
32 private readonly Dictionary<BindingSource, bool> _sources = new Dictionary<BindingSource, bool>();
33 #endregion
34 #region Events
39 #endregion
40 #region Constructors
45 {
46 InitializeComponent();
47 }
52 public BindingSourceRefresh(IContainer container)
53 {
54 container.Add(this);
55 InitializeComponent();
56 }
57 #endregion
58
59 #region Designer Functionality
63 private System.ComponentModel.IContainer components = null;
68 protected override void Dispose(bool disposing)
69 {
70 if (disposing && (components != null))
71 {
72 components.Dispose();
73 }
74 base.Dispose(disposing);
75 }
76 #region Component Designer generated code
81 private void InitializeComponent()
82 {
83 components = new System.ComponentModel.Container();
84 }
85 #endregion
86 #endregion
87 #region Public Methods
88
94 public bool CanExtend(object extendee)
95 {
96 return (extendee is BindingSource);
97 }
103 [Category("Csla")]
104 public bool GetReadValuesOnChange(BindingSource source)
105 {
106 bool result;
107 if (_sources.TryGetValue(source, out result))
108 return result;
109 else
110 return false;
111 }
119 [Category("Csla")]
121 BindingSource source, bool value)
122 {
123 _sources[source] = value;
124 if (!_isInitialising)
125 {
126 RegisterControlEvents(source, value);
127 }
128 }
129 #endregion
130
131 #region Properties
132
136 [Browsable(false)]
137 [DefaultValue(null)]
138#if NETSTANDARD2_0 || NET5_0
139 [System.ComponentModel.DataAnnotations.ScaffoldColumn(false)]
140#endif
141 public ContainerControl Host { get; set; }
142
146 [Browsable(true)]
147 [DefaultValue(false)]
148 public bool RefreshOnException { get; set; }
149
150 #endregion
151
152 #region Private Methods
159 private void RegisterControlEvents(ICurrencyManagerProvider container, bool register)
160 {
161 var currencyManager = container.CurrencyManager;
162 // If we are to register the events the do so.
163 if (register)
164 {
165 currencyManager.Bindings.CollectionChanged += Bindings_CollectionChanged;
166 currencyManager.Bindings.CollectionChanging += Bindings_CollectionChanging;
167 }
168 // Else unregister them.
169 else
170 {
171 currencyManager.Bindings.CollectionChanged -= Bindings_CollectionChanged;
172 currencyManager.Bindings.CollectionChanging -= Bindings_CollectionChanging;
173 }
174 // Reigster the binding complete events for the currencymanagers bindings.
175 RegisterBindingEvents(currencyManager.Bindings, register);
176 }
177
178
184 private void RegisterBindingEvents(BindingsCollection source, bool register)
185 {
186 foreach (Binding binding in source)
187 {
188 RegisterBindingEvent(binding, register);
189 }
190 }
196 private void RegisterBindingEvent(Binding binding, bool register)
197 {
198 if (register)
199 {
200 binding.BindingComplete += Control_BindingComplete;
201 }
202 else
203 {
204 binding.BindingComplete -= Control_BindingComplete;
205 }
206 }
207 #endregion
208 #region Event Methods
209
217 private void Bindings_CollectionChanging(object sender, CollectionChangeEventArgs e)
218 {
219 switch (e.Action)
220 {
221 case CollectionChangeAction.Refresh:
222 // remove events for entire list
223 RegisterBindingEvents((BindingsCollection)sender, false);
224 break;
225 case CollectionChangeAction.Add:
226 // adding new element - remove events for element
227 RegisterBindingEvent((Binding)e.Element, false);
228 break;
229 case CollectionChangeAction.Remove:
230 // removing element - remove events for element
231 RegisterBindingEvent((Binding)e.Element, false);
232 break;
233 }
234 }
235
243 private void Bindings_CollectionChanged(object sender, CollectionChangeEventArgs e)
244 {
245 switch (e.Action)
246 {
247 case CollectionChangeAction.Refresh:
248 // refresh entire list - add event to all items
249 RegisterBindingEvents((BindingsCollection)sender, true);
250 break;
251 case CollectionChangeAction.Add:
252 // new element added - add event to element
253 RegisterBindingEvent((Binding)e.Element, true);
254 break;
255 case CollectionChangeAction.Remove:
256 // element has been removed - do nothing
257 break;
258 }
259 }
260
269 private void Control_BindingComplete(object sender, BindingCompleteEventArgs e)
270 {
271 switch (e.BindingCompleteState)
272 {
273 case BindingCompleteState.Exception:
275 && e.Binding.DataSource is BindingSource
276 && GetReadValuesOnChange((BindingSource)e.Binding.DataSource))
277 {
278 e.Binding.ReadValue();
279 }
280 if (BindingError != null)
281 {
282 BindingError(this, new BindingErrorEventArgs(e.Binding, e.Exception));
283 }
284 break;
285 default:
286 if ((e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate)
287 && e.Binding.DataSource is BindingSource
288 && GetReadValuesOnChange((BindingSource)e.Binding.DataSource))
289 {
290 e.Binding.ReadValue();
291 }
292 break;
293 }
294 }
295
296 #endregion
297 #region ISupportInitialize Interface
298 private bool _isInitialising = false;
302 void ISupportInitialize.BeginInit()
303 {
304 _isInitialising = true;
305 }
310 void ISupportInitialize.EndInit()
311 {
312 _isInitialising = false;
313 foreach (var source in _sources)
314 {
315 if (source.Value)
316 RegisterControlEvents(source.Key, true);
317 }
318 }
319 #endregion
320 }
321
322 #region Delegates
323
329 public delegate void BindingErrorEventHandler(object sender, BindingErrorEventArgs e);
330
331 #endregion
332
333 #region BindingErrorEventArgs Class
334
338 public class BindingErrorEventArgs : EventArgs
339 {
340
341 #region Property Fields
342
343 private Exception _exception = null;
344 private Binding _binding = null;
345
346 #endregion
347
348 #region Properties
349
354 {
355 get { return (_exception); }
356 }
357
362 {
363 get { return (_binding); }
364 }
365
366 #endregion
367
368 #region Constructors
369
375 public BindingErrorEventArgs(Binding binding, Exception exception)
376 {
377 _binding = binding;
378 _exception = exception;
379 }
380
381 #endregion
382
383 }
384
385 #endregion
386}
BindingErrorEventArgs defines the event arguments for reporting a data binding error due to a excepti...
BindingErrorEventArgs(Binding binding, Exception exception)
Constructor creates a new BindingErrorEventArgs object instance using the information specified.
Binding Binding
Binding gets the binding that caused the exception.
Exception Exception
Exception gets the exception that caused the binding error.
BindingSourceRefresh contains functionality for refreshing the data bound to controls on Host as well...
BindingSourceRefresh()
Constructor creates a new BindingSourceRefresh component then initialises all the different sub compo...
override void Dispose(bool disposing)
Clean up any resources being used.
void SetReadValuesOnChange(BindingSource source, bool value)
SetReadValuesOnChange() sets the value of the custom ReadValuesOnChange extender property added to ex...
BindingSourceRefresh(IContainer container)
Constructor creates a new BindingSourceRefresh component, adds the component to the container supplie...
bool GetReadValuesOnChange(BindingSource source)
GetReadValuesOnChange() gets the value of the custom ReadValuesOnChange extender property added to ex...
bool CanExtend(object extendee)
CanExtend() returns true if extendee is a binding source.
bool RefreshOnException
Forces the binding to re-read after an exception is thrown when changing the binding value
ContainerControl Host
Not in use - kept for backward compatibility
BindingErrorEventHandler BindingError
BindingError event is raised when a data binding error occurs due to a exception.
delegate void BindingErrorEventHandler(object sender, BindingErrorEventArgs e)
BindingErrorEventHandler delegates is the event handling definition for handling data binding errors ...