Bindery aims to support fluent MVVM binding definition for WinForms applications.
- Bindery:
- Contains the static
Createfactory class - Dependent on the
Rx-Linqpackage
- Contains the static
- Bindery.Commands:
- Contains
CommandBaseandEnablableCommandBase(abstract base implementations of ICommand) - Dependent on the
Rx-Linqpackage
- Contains
- 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.
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));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.RegisterDisposable(disposableViewModel, disposableCommand);// 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); binder.Control(textBox).Property(c => c.Text).Get(vm => vm.Age, Convert.ToString);This also "binds" the control's Enabled property to the command's CanExecute method.
ICommand command = new CommandImplementation(viewModel);
binder.Control(button).OnClick(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);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);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);binder.OnPropertyChanged(vm => vm.ErrorMessage).Subscribe(msg => DisplayErrorDialog(msg));binder.Control(cancelButton).OnClick().Subscribe(ctx => form.Close());binder.Control(form).OnEvent("Closed").Subscribe(ctx => binder.Dispose());binder.Observe(viewModel.Observable).Execute(command);binder.Observe(viewModel.Observable).ObserveOn(Scheduler.Immediate).Execute(command);binder.Observe(viewModel.Observable).Subscribe(
ctx=>ctx.OnNext(oVal => OnNextAction(oVal))
.OnError(ex => HandleException(ex))
.OnComplete(() => OnCompleteAction()));IObservable<string> mouseMoveButtons =
Create.ObservableFor(form).Event<MouseEventArgs>("MouseMove")
.Select(ctx => Convert.ToString(ctx.Args.Button));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 returntruebut can be overridden by an inheriting class.- The
CanExecuteChangedevent has a protectedOnCanExecuteChanged()invocator method. - A new
ObserveCanExecuteChanges()method creates anIObservable<bool>that publishes the value ofCanExecute()each time it changes, assuming theCanExecuteChangedevent is triggered properly.
EnablableCommandBase is an abstract implementation of CommandBase that exposes an Enabled property. The Enabled property:
- Controls the return value of
CanExecute() - Raises the
CanExecuteChangedevent when its value changes