ViewModelBinding class Core

Binds the controls of a board to a view model (INotifyPropertyChanged), typed and in both directions: board.Bind(viewModel, x => x.Name).

The property is named by the lambda itself (CallerArgumentExpression, as a column's field is): x => x.Name listens for "Name" and writes through the property's setter. A change of the view model, on any thread, reaches the board on its UI thread inside Board.Mutate, so only what it changed repaints.

var name = board.Bind(new TextBox(), customer, x => x.Name);
var active = board.Bind(customer, x => x.Active);
board.Add(name, 0, 0)
     .Add(new CheckBox { Text = () => "Active", Value = active.Get, Toggle = active.Toggle }, 1, 0);

Call it on the UI thread: the board's bindings remember it as the way back for changes that arrive elsewhere while the board is not shown.

public static class ViewModelBinding
Namespace
ArionUI.Presentation.Controls
Package
ArionUI.Presentation · dotnet add package ArionUI.Presentation --prerelease

Methods

Bind

public static Bound<TValue> Bind<TViewModel, TValue>(this Board board, TViewModel viewModel, Func<TViewModel, TValue> property, string? propertyExpression = null) where TViewModel : class, INotifyPropertyChanged;

Mirrors the property property names (x => x.Name) for this board's controls: hand Bound.Get and Bound.Set (or ViewModelBinding.Toggle) to a control's value delegates.

Parameters

viewModel

The view model.

property

The property, as x => x.Property.

propertyExpression

Filled in by the compiler.

Exceptions

ArgumentException

The lambda is not x => x.Property, or the view model has no such public property.

Bind

public static ComboBox Bind<TViewModel>(this Board board, ComboBox combo, TViewModel viewModel, Func<TViewModel, string?> property, string? propertyExpression = null) where TViewModel : class, INotifyPropertyChanged;

Binds the value of combo to a string property in both directions: a pick (or, in an editable box, each edit) is written to the view model, and a change of the view model shows as the box's value - for an editable box that holds the focus, when the focus leaves or Enter commits.

Returns

The combo box, to place it on the board in the same expression.

Bind

public static TextBox Bind<TViewModel>(this Board board, TextBox box, TViewModel viewModel, Func<TViewModel, string?> property, BindingUpdate updateOn = default, string? propertyExpression = null) where TViewModel : class, INotifyPropertyChanged;

Binds the text of box to a string property in both directions: each edit is written to the view model, and a change of the view model replaces the text (keeping the caret while the box has the focus). What the view model makes of the user's own typing shows when the focus leaves or Enter commits, unless updateOn says BindingUpdate.Keystroke.

Returns

The text box, to place it on the board in the same expression.

Bind

public static TControl Bind<TControl, TViewModel, TValue>(this Board board, TControl control, IBindingAdapter<TControl, TValue> adapter, TViewModel viewModel, Func<TViewModel, TValue> property, string? propertyExpression = null) where TControl : Control where TViewModel : class, INotifyPropertyChanged;

Binds control, which holds its value itself, to the property property names, through adapter: the control shows the view model's value at once and on every change, and what the user enters goes back to the view model with the input that changed it. A control bound before lets go of its old binding.

Returns

The control, to place it on the board in the same expression.

Exceptions

ArgumentException

The property has a setter of another type than TValue: the edits could not be written back.

Toggle

public static void Toggle(this Bound<bool> bound);

Flips a bound flag: the Toggle of a CheckBox or ToggleSwitch (Toggle = active.Toggle).