Skip to content

lucasteles/ModelViewBinder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Build status License Nuget

ModelViewBinder

ModelViewBinder aims to support a simple two way binding definition.

*(For Windows Forms see the section bellow)

Installing

To install from Nuget

Install-Package ModelViewBinder 

Projects

  • ModelViewBinder:

    • Contains the base binder tool
  • ModelViewBinder.Forms:

    • Contains a implementation for winforms

Code Examples

Basic Binding

Create a root binder for the view model
var binder = new ModelViewBinder<SourceType>(source);
Dispose of a binder

Diposing of a binder removes all bindings and disposes of all subcriptions created by the binder.

binder.Dispose();
Register external disposables with the binder

This can be used to tie the lifetime of other objects to the binder's lifetime.

binder.RegisterDispose(disposable);
Bind a view model property to other class property
 binder.Bind(src => src.A, target, tgt => tgt.X);
Multiple Binds
 binder
    .Bind(src => src.A, target, tgt => tgt.X)
    .Bind(src => src.B, target, tgt => tgt.Y)
 ;
Bind an integer view model property to a string property
 binder.Bind(src => src.A, target, tgt => tgt.X, Convert.ToString);
Two way bind of an integer view model property to a string property
 binder.Bind(src => src.A, target, tgt => tgt.X, Convert.ToString, int.Parse);
Update all view model properties
 binder.FillSource();
Update all target properties
 binder.FillTargets();
Automatic update View Model when target changes

You have to implement the ITargetWithChangeEvent interface and fire the ValueChanged event

More simple bind

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);
Enable or disable targets

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();
Bind Callback
   binder.Bind(src => src.A, target, tgt => tgt.X).Then(() => /* ... */);
Interface for Dependency Injection

If do you want to inject the ModelViewBinder<SourceType> as a dependency you can use IModelViewBinder<SourceType> interface.

Windows Forms Binder Nuget

Installing

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

About

A simple model view binder for .NET

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages