In a previous article we discussed How to update control state after a command finishes. Now we will build on that and create a busy indicator.
Please check the previous article for some of the necessary code. Basically we need an IsBusy
property in our view model and toggle that property before and after any work is done.
By using that as a basis, we just add a ProgressBar inside a StatusBar to our view and bind to our IsBusy
property.
<StatusBar Height="28">
<ProgressBar
IsIndeterminate="True"
Width="100"
Height="20"
Visibility="{Binding IsBusy, Converter={my:BoolVisibilityConverter}}" />
</StatusBar>
And the code for the converter:
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BoolVisibilityConverter : ConverterMarkupExtension<BoolVisibilityConverter>
{
public BoolVisibilityConverter()
{
}
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is bool)
{
if ((bool)value)
{
return Visibility.Visible;
}
}
return Visibility.Collapsed;
}
}
We are making use of the same logic we discussed in the How to use converters without creating static resources article.
The result: