如何绑定到WPF ProgressBar的Visibility元素?

时间:2012-07-19 06:30:00

标签: wpf

如何绑定到WPF的ProgressBar可见性元素?我无法将它绑定到字符串属性,因为它需要一个包含三个选项的枚举:折叠,可见和隐藏。 请显示ProgressBar的绑定可见性的可理解代码。提前谢谢。

1 个答案:

答案 0 :(得分:6)

<Grid Width="150" Margin="30,0" Visibility="{Binding ProgressBarVisibility}">
    <ProgressBar Width="150" Height="Auto" HorizontalAlignment="Stretch" Foreground="#FF01D328"
                 Minimum="-3" Maximum="100" Value="{Binding DownloadPercentage, Mode=OneWay}" />
    <TextBlock Text="Downloading" HorizontalAlignment="Center" />
</Grid> 

在viewmodel中:

public Visibility ProgressBarVisibility
{
    get
    {
        return (DownloadingPdf && DownloadPercentage < 100) ? Visibility.Visible : Visibility.Collapsed;
    }
}

private int mDownloadPercentage;
public int DownloadPercentage
{
    get { return mDownloadPercentage; }
    set
    {
        if (mDownloadPercentage==value)
        {
            return;
        }
        else
        {
            mDownloadPercentage = Math.Min(Math.Abs(value), 100);
            OnPropertyChanged("DownloadPercentage");
            OnPropertyChanged("DownloadProgressBarVisibility");
        }
    }
}