Skip to content
shockzinfinityPublic

About

Fluent MVVM binding for WinForms

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Bindery

Bindery aims to support fluent MVVM binding definition for WinForms applications.

Projects

  • Bindery:
    • Contains the static Create factory class
    • Dependent on the Rx-Linq package
  • Bindery.Commands:
    • Contains CommandBase and EnablableCommandBase (abstract base implementations of ICommand)
    • Dependent on the Rx-Linq package

Assumptions

  • A view model is a binding source, an object of any type. Full binding functionality requires a view model to properly implement System.ComponentModel.INotifyPropertyChanged.
  • A target is a binding target, an object of any type. A target only supports a limited set of binding functionality.
  • A control is a target that implements System.Windows.Forms.IBindableComponent. A control supports the full range of binding functionality.
  • A command is an object that implements System.Windows.Input.ICommand.

Code Examples

Binding

Create a root binder for the view model
var binder = Create.Binder(viewModel);
Create a root binder and set the default subscription scheduler to schedule actions on the form's thread
var binder = Create.Binder(viewModel, new ControlScheduler(form));
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.RegisterDisposable(disposableViewModel, disposableCommand);
Bind a view model property to a control property
// Two-way binding
binder.Control(textBox).Property(c => c.Text).Bind(vm => vm.Name); 
// One-way binding from source to control
binder.Control(form).Property(c => c.UseWaitCursor).Get(vm => vm.IsBusy); 
// One-way binding from control to source
binder.Control(textBox).Property(c => c.Text).Set(vm => vm.Name); 
Bind an integer view model property to a string control property
binder.Control(textBox).Property(c => c.Text).Get(vm => vm.Age, Convert.ToString);
Bind a button's Click event to a command

This also "binds" the control's Enabled property to the command's CanExecute method.

ICommand command = new CommandImplementation(viewModel);
binder.Control(button).OnClick(command);
Bind a form's MouseMove event to a command
ICommand command = new CommandImplementation(viewModel);
binder.Control(form).OnEvent<MouseEventArgs>("MouseMove")
  .Transform(o => o.Where(ctx => ctx.Args.Button==MouseButtons.Left).Select(ctx => new {ctx.Args.X, ctx.Args.Y})) 
  // Mouse coords are passed to command.Execute()
  .Execute(command);
Bind a form's MouseMove event arguments to a view model property
binder.Control(form).OnEvent<MouseEventArgs>("MouseMove")
  .Transform(o => o.Select(ctx => new MyCoord{X = ctx.Args.X, Y = ctx.Args.Y}))
  .Set(vm => vm.CurrentMouseCoords);
Bind to a non-control target object

Non-control targets support a limited set of binding options. Two-way binding and one-way binding from target to source are not supported.

binder.Target(target).Property(t => t.Status).Get(vm => vm.Status);

Observable subscriptions

Trigger an action when a view model property changes
binder.OnPropertyChanged(vm => vm.ErrorMessage).Subscribe(msg => DisplayErrorDialog(msg));
Subscribe to a button's Click event to close the form
binder.Control(cancelButton).OnClick().Subscribe(ctx => form.Close());
Subscribe to a form's Closed event to dispose of the binder
binder.Control(form).OnEvent("Closed").Subscribe(ctx => binder.Dispose());
Create an observable subscription to execute a command
binder.Observe(viewModel.Observable).Execute(command);
Overriding the default scheduler to execute the command immediately on each observed object
binder.Observe(viewModel.Observable).ObserveOn(Scheduler.Immediate).Execute(command);
Subscribe to an observable with full subscription syntax support
binder.Observe(viewModel.Observable).Subscribe(
  ctx=>ctx.OnNext(oVal => OnNextAction(oVal))
       .OnError(ex => HandleException(ex))
       .OnComplete(() => OnCompleteAction()));

Event to observable conversion

IObservable<string> mouseMoveButtons =
  Create.ObservableFor(form).Event<MouseEventArgs>("MouseMove")
       .Select(ctx => Convert.ToString(ctx.Args.Button));

Commands

CommandBase

CommandBase is an abstract implementation of System.Windows.Input.ICommand designed to be useful for MVVM command implementation.

  • Execute() is abstract and must be implemented by an inheriting class.
  • CanExecute() is implemented to return true but can be overridden by an inheriting class.
  • The CanExecuteChanged event has a protected OnCanExecuteChanged() invocator method.
  • A new ObserveCanExecuteChanges() method creates an IObservable<bool> that publishes the value of CanExecute() each time it changes, assuming the CanExecuteChanged event is triggered properly.
EnablableCommandBase

EnablableCommandBase is an abstract implementation of CommandBase that exposes an Enabled property. The Enabled property:

  • Controls the return value of CanExecute()
  • Raises the CanExecuteChanged event when its value changes

About

Fluent MVVM binding for WinForms

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages