databinding refactoring

  • Thread starter Thread starter Berryl Hesh
  • Start date Start date
B

Berryl Hesh

Please tell me how to change the code below to use data binding instead of
manual observer synchronization. My domain model class (Reading) implements
INotifyPropertyChanged

Thanks! - BH
------

public partial class Forms__Controls_AssessmentForm : Form
{
private readonly Reading _model;

public Forms__Controls_AssessmentForm(Reading model) {
InitializeComponent();
_model = model;
txtActual.Validating += new
System.ComponentModel.CancelEventHandler(txtActual_Validating);
_load();
}

void txtActual_Validating(object sender,
System.ComponentModel.CancelEventArgs e) {
_displayVariance();
}

private void _load() {
txtStationId.Text = _model.StationId;
txtDate.Text = _model.Date.ToString();
txtStationId.Text = _model.StationId;
txtTarget.Text = _model.TargetTemperature.ToString();
txtActual.Text = _model.ActualTemperature.ToString();
_displayVariance();
}

private void _displayVariance() {
_model.ActualTemperature = Convert.ToDouble(txtActual.Text);
lblVariance.Text = string.Format("{0:P0}", _model.Variance);
LabelSeverityDisplay.Severity(Reading.DetermineSeverity(_model.Variance),
lblVariance);
}
}
 
Berryl Hesh said:
Please tell me how to change the code below to use data binding instead of
manual observer synchronization. My domain model class (Reading) implements
INotifyPropertyChanged

Thanks! - BH
------

public partial class Forms__Controls_AssessmentForm : Form
{
private readonly Reading _model;

public Forms__Controls_AssessmentForm(Reading model) {
InitializeComponent();
_model = model;
txtActual.Validating += new
System.ComponentModel.CancelEventHandler(txtActual_Validating);
_load();
}

void txtActual_Validating(object sender,
System.ComponentModel.CancelEventArgs e) {
_displayVariance();
}

private void _load() {
txtStationId.Text = _model.StationId;
txtDate.Text = _model.Date.ToString();
txtStationId.Text = _model.StationId;
txtTarget.Text = _model.TargetTemperature.ToString();
txtActual.Text = _model.ActualTemperature.ToString();
_displayVariance();
}

private void _displayVariance() {
_model.ActualTemperature = Convert.ToDouble(txtActual.Text);
lblVariance.Text = string.Format("{0:P0}", _model.Variance);
LabelSeverityDisplay.Severity(Reading.DetermineSeverity(_model.Variance),
lblVariance);
}
}

txtStationId.DataBindings.Add("Text", _model, "StationId");
txtDate.DataBindings.Add("Text", _model, "Date");
....
Binding binding = new Binding("Text", _model, "Variance", true);
binding.Format += new ConvertEventHandler(binding_Format);
lblVariance.DataBindings.Add(binding);

....

void binding_Format(object sender, ConvertEventArgs e)
{
double variance = (double)e.Value;
e.Value = string.Format("{0:P0}", variance);
}

I would probably calculate ActualTemperature in the property getter.
 
Back
Top