Home  >  Article  >  Backend Development  >  How to implement a simple progress bar in WPF?

How to implement a simple progress bar in WPF?

零下一度
零下一度Original
2017-06-23 15:17:554520browse

I was working on a project recently, and I saw that the progress bar written by a former colleague was very effective, so I simplified it. It is not dazzling, but it is still sufficient for the project.

Still, let’s take a look at the effect after the call.

1. Because the Foreground display of ProgressbBar must be the same, there must be a parameter to Control is set, so a parameter value 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;
    }
}

is defined. There is such a sentence in the code "LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;" for the convenience of passing This is the parameter used to get the style from the style file.

<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. Since it is a ProgressBar, it must have a progress value. We use TextBlock to display this value. We must implement the notification interface to ensure real-time notification to the page. superior.

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

3. Enable a background thread to continuously update the progress effect

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 + "";
}

Source code

The above is the detailed content of How to implement a simple progress bar in WPF?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn