Binding to Tasks and Observables
You can subscribe to the result of a task or an observable by using the ^ stream binding operator.

Example 1: Binding to an observable

For example if DataContext.Name is an IObservable<string> then the following example will bind to the length of each string produced by the observable as each value is produced
1
<TextBlock Text="{Binding Name^.Length}"/>
Copied!

Example 2: Binding to a task

If you need to do some heavy work to load the content of a property you can bind to the result of an async Task<TResult>
Consider you have the following view model which generates some text in a long running process:
1
public Task<string> MyAsyncText => GetTextAsync();
2
​
3
private async Task<string> GetTextAsync()
4
{
5
await Task.Delay(1000); // The delay is just for demonstration purpose
6
return "Hello from async operation";
7
}
Copied!
You can bind to the result in the following way:
1
<TextBlock Text="{Binding MyAsyncText^, FallbackValue='Wait a second'}" />
Copied!
Note: You can use FallbackValue to display some loading indicator.
Export as PDF
Copy link
Edit on GitHub