Use Property<T> and ReadOnlyProperty<T> to create bindable properties in your ViewModel. These implement IProperty<T> and IReadOnlyProperty<T>, which expose a ValueChanged event for UI updates.
Simple Property
Directly instantiate a Property<T> in the constructor.
Observable Property with Attributes
You can use the [Observable] attribute on private fields to automatically map them to public binding paths. The toolkit automatically converts field names like _title or m_title to Title for the binding path.
Wrapping non-observable models
To wrap an existing data model (like a database entity), use [Observable(nameof(PropertyName))] on a private IProperty<T> field and relay the getter/setter to the underlying model.
Using UnityMvvmToolkit.Generator
If using the UnityMvvmToolkit.Generator package, you can use the [WithObservableBackingField] attribute on a property to automatically generate the observable backing field, significantly reducing boilerplate.
// Simple Property
public class CounterViewModel : IBindingContext
{
public CounterViewModel()
{
Count = new Property<int>();
}
public IProperty<int> Count { get; }
}
// Observable Property
public class MyViewModel : IBindingContext
{
[Observable("Count")]
private readonly IProperty<int> _amount = new Property<int>();
[Observable]
private readonly IProperty<string> _title = new Property<string>();
}