ModelViewBinder aims to support a simple two way binding definition.
*(For Windows Forms see the section bellow)
To install from Nuget
Install-Package ModelViewBinder
-
ModelViewBinder:
- Contains the base binder tool
-
ModelViewBinder.Forms:
- Contains a implementation for winforms
var binder = new ModelViewBinder<SourceType>(source);
Diposing of a binder removes all bindings and disposes of all subcriptions created by the binder.
binder.Dispose();
This can be used to tie the lifetime of other objects to the binder's lifetime.
binder.RegisterDispose(disposable);
binder.Bind(src => src.A, target, tgt => tgt.X);
binder
.Bind(src => src.A, target, tgt => tgt.X)
.Bind(src => src.B, target, tgt => tgt.Y)
;
binder.Bind(src => src.A, target, tgt => tgt.X, Convert.ToString);
binder.Bind(src => src.A, target, tgt => tgt.X, Convert.ToString, int.Parse);
binder.FillSource();
binder.FillTargets();
You have to implement the ITargetWithChangeEvent
interface and fire the ValueChanged
event
If are you working with some type of control, you can implement ITargetWithValue<type>
or ITargetWithValue
, it has a Value
property which is automatically recognized by the binder
binder.Bind(src => src.A, target);
If are you working with some type of control, you can implement ITargetWithEnabled
, it has a Enabled property, commonly used in UI controls, if you want to set this property of all binded targets you can use:
For disable
binder.DisableAll();
For enable
binder.EnableAll();
binder.Bind(src => src.A, target, tgt => tgt.X).Then(() => /* ... */);
If do you want to inject the ModelViewBinder<SourceType>
as a dependency you can use IModelViewBinder<SourceType>
interface.
To install from Nuget
Install-Package ModelViewBinder.Forms
For window form you should use FormModelViewBinder<SourceType>
var source = new Source();
var binder = new FormModelViewBinder<Source>(source);
binder
.Bind(src => src.ValueForTextbox, textBox1, tgt => tgt.Text)
.Bind(src => src.ValueForRichText, richTextBox1, tgt => tgt.Text)
.Bind(src => src.ValueForComboBox, comboBox1, tgt => tgt.SelectedValue)
.Bind(src => src.ValueForUpDown, numericUpDown1, tgt => tgt.Value)
.Bind(src => src.ValueForDatePicker, dateTimePicker1, tgt => tgt.Value)
.Bind(src => src.ValueForCheckBox, checkBox1, tgt => tgt.Checked)
;
binder.FillTargets();
The FormModelViewBinder
automatically will bind the change events of the forms controls fot update de view model.
if you dont want this behavior you can disable:
binder.AutoFillSourceWhenTargetChanges = false;
For targets derivated of Windows.Form.Control, you dont need to implement ITargetWithEnabled
, the methods EnableAll
and DisableAll
will recognize the Control and change properly the Enabled
property
Tks