You can leverage IsAsync property of Binding
From MSDN:
Use the IsAsync property when the get accessor of your binding source
property might take a long time. One example is an image property with
a get accessor that downloads from the Web. Setting IsAsync to true
avoids blocking the UI while the download occurs.
example
<Image Source="{Binding MyImage,IsAsync=True, Converter={StaticResource MyConverter}}" />
more on Binding.IsAsync
Async Converter
I managed to create a async converter
namespace CSharpWPF
{
class AsyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new AsyncTask(() =>
{
Thread.Sleep(4000); //long running job eg. download image.
return "success";
});
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public class AsyncTask : INotifyPropertyChanged
{
public AsyncTask(Func<object> valueFunc)
{
AsyncValue = "loading async value"; //temp value for demo
LoadValue(valueFunc);
}
private async Task LoadValue(Func<object> valueFunc)
{
AsyncValue = await Task<object>.Run(()=>
{
return valueFunc();
});
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("AsyncValue"));
}
public event PropertyChangedEventHandler PropertyChanged;
public object AsyncValue { get; set; }
}
}
}
this converter will return an instance of AsyncTask which will encapsulate the long running job within
class AsyncTask will execute the task asynchronously and will set the result to AsyncValue as it also implements INotifyPropertyChanged hence using the notification to update the UI
usage
<Grid xmlns:l="clr-namespace:CSharpWPF">
<Grid.Resources>
<l:AsyncConverter x:Key="AsyncConverter" />
</Grid.Resources>
<TextBlock DataContext="{Binding MyProperty,Converter={StaticResource AsyncConverter}}"
Text="{Binding AsyncValue}" />
</Grid>
Idea is to bind the DataContext of the element to the converter and the desired property to the AsyncValue of the new data context
above example is using Text property of a text block for easy demo
exmaple is made for a IValueConverter same approach can be used for IMultiValueConverter too.