我会使用标准的 BooleanToVisiblityConverter 并将 Visibilty 绑定到 IsBusy 属性,如下所示:
<Grid Height="500" Width="500" Background="Blue">
<Grid.Resources>
<Converters:BoolToVisConverter x:Key="BoolToVis"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Width="75">
<ToggleButton x:Name="BusyButton" Content="Toggle Busy State"/>
<ToggleButton x:Name="ProgressButton" Content="Toggle ProgressButton State"/>
</StackPanel>
<toolkit:BusyIndicator Grid.Row="1" IsBusy="{Binding IsChecked, ElementName=BusyButton}"
Visibility="{Binding IsBusy, RelativeSource={RelativeSource Self}}"/>
<ProgressBar Grid.Row="2" Width="120" Height="10" Margin="4 2" VerticalAlignment="Center" IsIndeterminate="True"
Visibility="{Binding IsChecked, ElementName=ProgressButton, Converter={StaticResource BoolToVis}}"/>
</Grid>
我已经提供了 BusyIndicator 以及 ProgressBar 是这个示例,因此您可以看到两者的实际效果。
BooleanToVisibilityConverter 是非常标准的,并且是这样实现的:
public class BoolToVisConverter : IValueConverter
{
#region IValueConverter Members
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return Visibility.Collapsed;
return (bool)value == true ? Visibility.Visible : Visibility.Collapsed;
}
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}