如何在Value为0时创建隐藏的ProgressBar?

时间:2011-07-20 22:45:04

标签: c# wpf xaml data-binding

以下内容不太有用(ProgressValue是DataContext中设置的值。)

<StatusBarItem Grid.Column="1">
    <StatusBarItem.Resources>
        <Style TargetType="ProgressBar">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ProgressValue}" Value="0">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StatusBarItem.Resources>
    <ProgressBar Value="{Binding ProgressValue}" Grid.Column="1" Width="80" Height="13">
    </ProgressBar>
</StatusBarItem>

3 个答案:

答案 0 :(得分:6)

尝试在Visibility事件中设置ProgressBar的ValueChanged属性:

private void progressBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
    progressBar1.Visibility = (progressBar1.Value == 0) ? Visibility.Hidden : Visibility.Visible;
}

答案 1 :(得分:4)

尝试指定最小(和最大)值。设置后,它似乎适用于我的代码版本。

使用此XAML:

    <StatusBarItem x:Name="Feedback" Grid.Row="1">
        <StatusBarItem.Resources>
            <Style TargetType="ProgressBar">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding}" Value="0">
                        <Setter Property="Visibility" Value="Hidden"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StatusBarItem.Resources>
        <ProgressBar Value="{Binding Mode=OneWay}" Minimum="0" Maximum="10" Grid.Column="1" Width="80" Height="13">
        </ProgressBar>
    </StatusBarItem>

和MainWindows.xaml.cs构造函数中的此代码(仅用于测试目的)

    int value = 10;
    public MainWindow()
    {
        InitializeComponent();
        Feedback.DataContext = value;

        Timer t = new Timer(500);
        t.Elapsed += (s, e) =>
            {
                if (value > 0) Dispatcher.Invoke(new Action(() => { Feedback.DataContext = --value; }));
                else t.Stop();
            };

        t.Start();
    }

我得到进度条从10下降到0,此时进度条消失。

答案 2 :(得分:0)

示例XAML:

<ProgressBar>
    <ProgressBar.Resources>
        <src:DoubleToVisibilityConverter x:Key="_doubleToVisibilityConverter" />
    </ProgressBar.Resources>
    <ProgressBar.Visibility>
        <Binding
            RelativeSource="{RelativeSource Self}"
            Path="Value"
            Converter="{StaticResource _doubleToVisibilityConverter}"
        />
    </ProgressBar.Visibility>
</ProgressBar>

代码:

public class DoubleToVisibilityConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double input = 0;

        if (value is double)
        {
            input = (double)value;
        }
        else if (value is int)
        {
            input = (int)value;
        }
        else if (value is string) // Useful if input of converter is written in XAML
        {
            if (!double.TryParse((string)value, out input))
                return Binding.DoNothing;
        }
        else
        {
            return Binding.DoNothing;
        }

        return (input > 0 ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

免责声明:从内存中编写,未编译或测试。