using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows.Forms; using DelftTools.Controls.Swf.DataEditorGenerator.Binding; using DelftTools.Controls.Swf.DataEditorGenerator.Binding.ControlBindings; using DelftTools.Utils.Collections; namespace DelftTools.Controls.Swf.DataEditorGenerator { /// /// This class is the container of the user controls generated by the DataEditorGenerator. /// Add this to your view (Controls.Add(dataEditor)) and set its Data property. /// public class DataEditor : Panel { public ICollection Bindings { get; private set; } public DataEditor() { Dock = DockStyle.Fill; //default fill Bindings = new Collection(); } /// /// Gets the custom controls contained in this data editor. Provided for convenience: to /// perform any manual data binding / customizations etc. /// /// public IEnumerable GetCustomControls() { return Bindings.OfType().Select(ccb => ccb.EditControl); } private object data; public object Data { get { return data; } set { if (data != null) { var propertyChanged = data as INotifyPropertyChanged; if (propertyChanged != null) { propertyChanged.PropertyChanged -= DataPropertyChanged; } this.GetAllControlsRecursive().ForEach(a => a.CancelEdit()); foreach (var binding in Bindings) { binding.Data = null; } } data = value; if (data != null) { foreach (var binding in Bindings) binding.Data = data; foreach (var binding in Bindings) binding.Validate(binding.FieldDescription.GetValue(data)); var propertyChanged = data as INotifyPropertyChanged; if (propertyChanged != null) { propertyChanged.PropertyChanged += DataPropertyChanged; } } } } void DataPropertyChanged(object sender, PropertyChangedEventArgs e) { var dataAsEditableObject = data as Utils.Editing.IEditableObject; if (dataAsEditableObject != null && dataAsEditableObject.IsEditing) return; // still editing, skip foreach (var binding in Bindings) { binding.OnPropertyChanged(sender, e); } } } }