string _Property; public string Property { get { return this._Property; } set { this._Property = value; if (this.PropertyIsChanged != null) { this.PropertyIsChanged.Invoke(); } } } public event Action PropertyIsChanged;
Control _targetControl; /// <summary> /// Get; set-once. /// Control (), -. /// </summary> public Control TargetControl { get { return this._targetControl; } set { if (this._targetControl != null) { /* do nothing */ } else { this._targetControl = value; } } } string _targetControlProperty; /// <summary> /// Get; set-once. /// Control'a, /// -. /// </summary> public string TargetControlProperty { get { return this._targetControlProperty; } set { if (this._targetControlProperty != null) { /* do nothing */ } else { this._targetControlProperty = value; } } }
/// <summary> /// , Control. /// </summary> object _dataSourceObject; /// <summary> /// Get; set-once. /// -, /// Control. /// </summary> public Object DataSourceObject { get { return this._dataSourceObject; } set { if (this._dataSourceObject != null) { /* do nothing */ } else { this._dataSourceObject = value; } } } string _dataSourceObjectProperty; /// <summary> /// Get; set-once. /// , /// Control(). /// </summary> public string DataSourceObjectProperty { get { return this._dataSourceObjectProperty; } set { if (this._dataSourceObjectProperty != null) { /* do nothing */ } else { this._dataSourceObjectProperty = value; } } }
public SimpleBinder( Control targetControl, string targetControlProperty, object dataSourceObject, string dataSourceProperty, string dataSourcePropertyChanged = "") { // safety checks CheckIfPropertyExists(targetControl, targetControlProperty); CheckIfPropertyExists(dataSourceObject, dataSourceProperty); // end safety this._targetControl = targetControl; this._targetControlProperty = targetControlProperty; this._dataSourceObject = dataSourceObject; this._dataSourceObjectProperty = dataSourceProperty; if (dataSourcePropertyChanged == String.Empty) { this.Binding(); } else { CheckIfEventExists(dataSourceObject, dataSourcePropertyChanged); this.Binding(dataSourcePropertyChanged, null); } }
if (dataSourcePropertyChanged == String.Empty) { this.Binding(); } else { CheckIfEventExists(dataSourceObject, dataSourcePropertyChanged); this.Binding(dataSourcePropertyChanged, null); }
private void Binding() { this.Binding_SetValueToControl(this._targetControlProperty, this.DataSourceObjectProperty); }
private void Binding_SetValueToControl( string targetControlProperty, string dataSourceProperty) { this.TargetControl.GetType() .GetProperty(targetControlProperty) // .SetValue(this.TargetControl, this.DataSourceObject.GetType() .GetProperty(dataSourceProperty) // .GetValue(this.DataSourceObject) ); }
private void Binding_DataSourcePropertyChangedEvent( string dataSourcePropertyChanged, Delegate propertyChangedEventHandler = null ) { if (propertyChangedEventHandler != null) { this.DataSourceObject.GetType() .GetEvent(dataSourcePropertyChanged) .AddEventHandler(this.DataSourceObject, propertyChangedEventHandler); } else { SimpleBinder RefToThis = this; this.DataSourceObject.GetType() .GetEvent(dataSourcePropertyChanged) .AddEventHandler(this.DataSourceObject, new Action( () => { RefToThis.UpdateControl(RefToThis.GetDataSourcePropertyValue()); } )); } }
SourceObj = new SomeClass("Text?"); // -. "Text?" - . SimpleBinder txtBoxBinder = new SimpleBinder(this.label1, "Text", // Control , SourceObj, "Property", // "PropertyIsChanged"); // -.
Source: https://habr.com/ru/post/269627/
All Articles