Automatic Data Binding
Once you've created a full set of properties in the model and view, you can apply the data binding in the CalculatorForm. In Listing Three, you can bind the Text property of the text box to the Operand2 property of the model, and the Enabled property of the button to the CanDivide property of the model. These bindings enforce that when the Text in the view changes, the model is updated. When the CanDivide property changes, the Divide button is enabled/disabled.
public partial class CalculatorForm : Form { public CalculatorForm() { InitializeComponent(); InitializeBindings(); this.Calculator = new Calculator(); } private void InitializeBindings() { _divideButton.DataBindings.Add( "Enabled", _calculatorBindingSource, "CanDivide", false, DataSourceUpdateMode.OnPropertyChanged); _operand2TextBox.DataBindings.Add( "Text", _calculatorBindingSource, "Operand2", false, DataSourceUpdateMode.OnPropertyChanged); } ... }