A binding connects a property of a target (an element in the markup) to a property of a source (usually a view model) so that a change in one shows in the other. The markup says only what is connected and how; the objects, the change notification (INotifyPropertyChanged) and the code are elsewhere. A binding is a markup extension in an attribute, or a Binding object element in a property element.
<TextBox Text="{Binding Path=Customer.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True, TargetNullValue='(none)'}"/>
<TextBlock Text="{Binding Total, StringFormat='{}{0:C}'}" Visibility="{Binding HasItems, Converter={StaticResource BoolToVis}}"/>
<Slider x:Name="zoom"/><TextBlock Text="{Binding Value, ElementName=zoom}"/>
The Binding properties
| Property | Meaning | Frameworks |
|---|---|---|
Path | the property of the source, as a property path; the first positional argument; empty or . is the source itself | all |
Mode | OneWay (source to target), TwoWay, OneTime, OneWayToSource (target to source, WPF), Default (the property’s own default: TwoWay for TextBox.Text) | all (MAUI also Default, TwoWay, OneWay, OneWayToSource, OneTime) |
UpdateSourceTrigger | when a two-way binding writes back: PropertyChanged, LostFocus, Explicit, Default | WPF, UWP, WinUI, Avalonia |
Source | an object, usually {StaticResource …} or {x:Static …}, used instead of the data context | all |
ElementName | the name of an element in the same name scope as the source | WPF, UWP, WinUI; MAUI uses Source={x:Reference name}; Avalonia uses #name.Prop |
RelativeSource | a source by its place: Self, TemplatedParent, FindAncestor with AncestorType and AncestorLevel, PreviousData (WPF) | WPF (all four); UWP and WinUI (Self, TemplatedParent); MAUI (Self, TemplatedParent, FindAncestor, FindAncestorBindingContext); Avalonia uses $self, $parent[Type] |
Converter, ConverterParameter, ConverterCulture (WPF) / ConverterLanguage (UWP, WinUI) | an IValueConverter (Convert and ConvertBack), its parameter and the culture | all |
StringFormat | a .NET format string; '{}{0:C}' or 'Total: {0:N2}' (a format that starts with a brace needs the {} escape); only for a String target | WPF, MAUI, Avalonia; not in UWP’s Binding |
FallbackValue, TargetNullValue | the value when the binding cannot be resolved, and when the source value is null | all |
IsAsync, Delay, AsyncState | get the value on another thread; wait for a number of milliseconds before updating the source; a state for it | WPF (Delay also UWP, WinUI) |
BindingGroupName | a group of bindings validated and committed together | WPF |
BindsDirectlyToSource | bind to the data provider itself and not to its data | WPF |
XPath | an XPath expression, for XML sources (XmlDataProvider) | WPF |
NotifyOnSourceUpdated, NotifyOnTargetUpdated, NotifyOnValidationError | raise the SourceUpdated, TargetUpdated and Validation.Error events | WPF |
ValidatesOnDataErrors, ValidatesOnExceptions, ValidatesOnNotifyDataErrors, ValidationRules | take validation errors from IDataErrorInfo, from exceptions in the setter, from INotifyDataErrorInfo, and from the listed ValidationRule objects | WPF |
UpdateSourceExceptionFilter | a method that decides what to do with an exception while updating the source | WPF |
Where the source comes from
- the
Source,ElementNameorRelativeSourceof the binding, if it has one; - otherwise the DataContext (WPF, UWP, WinUI, Avalonia) or BindingContext (MAUI) of the element, which is inherited by the child elements: set it once on a root (
DataContext="{Binding}",<Window.DataContext><local:MainViewModel/></Window.DataContext>) and every binding below reads it; - in a data template, the item the template is made for.
If the path does not resolve, the property keeps its default value or the FallbackValue; WPF writes a message to the debug output, and the binding does not throw.
MultiBinding and PriorityBinding (WPF)
MultiBinding has a Converter (an IMultiValueConverter), a ConverterParameter, a StringFormat and a list of child Bindings whose values are passed to it together: <MultiBinding StringFormat="{}{0} {1}"><Binding Path="First"/><Binding Path="Last"/></MultiBinding>. PriorityBinding holds bindings in order of priority; the first that has a value is used. MAUI has MultiBinding with Bindings, Converter and StringFormat; Avalonia has MultiBinding and the & path operator.
x:Bind (UWP and WinUI)
{x:Bind} is compiled into code by the markup compiler. Its path starts at the page or control (its code-behind class), not at the data context, so {x:Bind ViewModel.Title} reads the property ViewModel of the page. It can bind to a method ({x:Bind Format(Value)}), to an element by its x:Name ({x:Bind zoom.Value}), to a static member ({x:Bind local:Util.Name}) and to a property of an x:DataType inside a template. The default mode is OneTime, and x:DefaultBindMode changes it below an element. BindBack names a method for two-way binding to a method or function. An event is wired by naming a method of the page: Click="{x:Bind Save}". x:Phase and x:Load let a recycling list defer work.
Compiled bindings in MAUI and Avalonia
MAUI’s x:DataType="local:MainViewModel" on a page, a template or an element switches its {Binding}s to compiled form and checks the paths when the markup is built. x:DataType="x:Null" switches it off below that element. Avalonia’s x:CompileBindings="True" on the root and x:DataType do the same, and {CompiledBinding} and {ReflectionBinding} choose the form for one binding.
Data providers and sources in markup (WPF)
ObjectDataProvider(ObjectType,MethodName,ConstructorParameters,MethodParameters) makes an object or calls a method for the data.XmlDataProvider(Sourceor an inline<x:XData>,XPath) provides XML data.CollectionViewSource(Source,SortDescriptions,GroupDescriptions) makes a sorted, filtered or grouped view of a collection.
Not described
Known, not yet covered
- Reference: Avalonia’s binding path operators and MAUI’s multi-binding and relative-source rules are summarised, not complete; validation rules (
ValidationRule,DataAnnotations) are named, not described. - Reference: the exact version in which a binding property appeared in UWP and WinUI is not given.
Unknown
- Reference: whether
StringFormatis supported by every version of the UWP and WinUIBindingwas not established; the page states that it is not in the documented property list.