WPF 簡易進度條效果


最近做一個項目,看到以前同事寫的進度條效果不錯,所以,拿來簡化了下,不炫,但是項目中還是夠用的。

還是,先來看下調用以后的效果

1、因為ProgressbBar的Foreground顯示不得不一樣,所以,要有一個參數去給控件進行設置,因此定義了一個參數值ForegroundColor

public int ForegroundColor
{
    get
    {
        return _foregroundColor;
    }

    set
    {
        _foregroundColor = value;
        LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;
        if (lgb != null)
            proBar.Foreground = txt.Foreground = percent.Foreground = lgb;
    }
}

代碼里有這么一句話“LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;”是為了方便通過這是這個參數去樣式文件里取樣式的。

<LinearGradientBrush x:Key="ForegroundColor1" EndPoint="1,0.5" StartPoint="0,0.5">
    <GradientStop Color="#FFBBF586" Offset="0.5"/>
    <GradientStop Color="#FFD4F9C3" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ForegroundColor2" EndPoint="1,0.5" StartPoint="0,0.5">
    <GradientStop Color="#FF5BE26E" Offset="0.5"/>
    <GradientStop Color="#FF8DEC9C" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ForegroundColor3" EndPoint="1,0.5" StartPoint="0,0.5">
    <GradientStop Color="#FFB656F2" Offset="0.5"/>
    <GradientStop Color="#FFAE8DFE" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ForegroundColor4" EndPoint="1,0.5" StartPoint="0,0.5">
    <GradientStop Color="#FF3AE9E9" Offset="0.5"/>
    <GradientStop Color="#FF8DFDFE" Offset="1"/>
</LinearGradientBrush>

2、既然是ProgressBar就要有一個進度值,這個值,我們用TextBlock來進行顯示,一定要實現通知接口,這樣,才能保證實時的通知到頁面上。

public string ValueText
{
    get
    {
        return _valueText;
    }

    set
    {
        _valueText = value;
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ValueText"));
        }
    }
}

3、啟用一個后台線程,來不斷的更新進度效果

private void Bgw_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i < BarValue; i++)
    {
        System.Threading.Thread.Sleep(50);
        proBar.Dispatcher.Invoke(new Action(
                                     delegate
        {
            if (proBar.Value <= BarValue)
            {
                proBar.Value++;
            }
        }));
        ValueText = i + "";
    }
    ValueText = BarValue + "";
}

源碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM