博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 进度条
阅读量:5035 次
发布时间:2019-06-12

本文共 4585 字,大约阅读时间需要 15 分钟。

//Create a Delegate that matches the Signature of the ProgressBar's SetValue method        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);        private void Process()        {            //Configure the ProgressBar            ProgressBar1.Minimum = 0;            ProgressBar1.Maximum = short.MaxValue;            ProgressBar1.Value = 0;            //Stores the value of the ProgressBar            double value = 0;            //Create a new instance of our ProgressBar Delegate that points            //  to the ProgressBar's SetValue method.            var updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar1.SetValue);            //Tight Loop:  Loop until the ProgressBar.Value reaches the max            do            {                value += 1;                /*Update the Value of the ProgressBar:		         1)  Pass the "updatePbDelegate" delegate that points to the ProgressBar1.SetValue method			      2)  Set the DispatcherPriority to "Background"				 3)  Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */                Dispatcher.Invoke(updatePbDelegate,                    System.Windows.Threading.DispatcherPriority.Background,                    new object[] {RangeBase.ValueProperty, value});            } while (ProgressBar1.Value <= ProgressBar1.Maximum); //
privatevoidWindow_ContentRendered(object sender,EventArgs e)                 {
                        BackgroundWorker worker =newBackgroundWorker();                         worker.WorkerReportsProgress=true;                         worker.DoWork+= worker_DoWork;                         worker.ProgressChanged+= worker_ProgressChanged;                         worker.RunWorkerAsync();                 }                 void worker_DoWork(object sender,DoWorkEventArgs e)                 {
                        for(int i =0; i <100; i++)                         {
                                (sender asBackgroundWorker).ReportProgress(i);                                 Thread.Sleep(100);                         }                 }                 void worker_ProgressChanged(object sender,ProgressChangedEventArgs e)                 {
                        pbStatus.Value= e.ProgressPercentage;                 }

Indeterminate

ProgressBar with text:

       
       
   

<Window x:Class="ProgressBarTutorial.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="240" Width="446" Loaded="Window_Loaded">
    <Grid>
        <ProgressBar Margin="10,10,0,13" Name="PBar" HorizontalAlignment="Left" 
                 VerticalAlignment="Top" Width="300" Height="30" Value="60" FlowDirection="LeftToRight">            
        </ProgressBar>
        <StatusBar Name="sbar" Grid.Column="0" Grid.Row="5" VerticalAlignment="Bottom" Background="Beige" >
            <StatusBarItem>
                <TextBlock>StatusBar</TextBlock>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>

cs:

       private Timer aTimer;

        public Window1()

        {
            InitializeComponent();
            CreateDynamicProgressBarControl();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {
            Duration duration = new Duration(TimeSpan.FromSeconds(20));
            DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
            PBar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
        }

        private void CreateDynamicProgressBarControl()

        {
            ProgressBar progbar = new ProgressBar();
            progbar.IsIndeterminate = false;
            progbar.Orientation = Orientation.Horizontal;
            progbar.Width = 150;
            progbar.Height = 15;
            Duration duration = new Duration(TimeSpan.FromSeconds(10));
            DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
            progbar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
            sbar.Items.Add(progbar);
        }

<StatusBar Name="sbar" Grid.Column="0" Background="Beige" Margin="8,171.04,-8,177" >

            <StatusBarItem>
                <TextBlock Width="110.657" Height="22.96">StatusBar</TextBlock>
            </StatusBarItem>
        </StatusBar>

 private void MakeIndeterminate(object sender, RoutedEventArgs e)

        {
            sbar.Items.Clear();
            Label lbl = new Label();
            lbl.Background = new LinearGradientBrush(Colors.Pink, Colors.Red, 90);
            lbl.Content = "Indeterminate ProgressBar.";
            sbar.Items.Add(lbl);
            ProgressBar progbar = new ProgressBar();
            //progbar.Background = Brushes.Gray;
            progbar.Foreground = Brushes.LightBlue;
            progbar.Width = 150;
            progbar.Height = 15;
            progbar.IsIndeterminate = true;
            sbar.Items.Add(progbar);
        }

 

转载于:https://www.cnblogs.com/wondaz/p/wpfprogressbar.html

你可能感兴趣的文章
NoClassDefFoundError: org.ksoap2.transport.HttpTransportSE
查看>>
关于MVC与MVP的理解
查看>>
PHP preg_match正则表达式
查看>>
Windows2008R2安装Exchange 2010前必须要做的准备工作
查看>>
jquery 无刷新加载执行,显示数据
查看>>
了解栈(顺序栈)的实现方法
查看>>
bzoj 3732 Network
查看>>
对象数组
查看>>
Hadoop创建/删除文件夹出错
查看>>
差速移动机器人之建模与里程计
查看>>
Django学习笔记
查看>>
150. Evaluate Reverse Polish Notation
查看>>
03-THREE.JS GUI使用
查看>>
Java基础知识强化之IO流笔记70:Properties练习之 如何让猜数字小游戏只能玩5次的案例...
查看>>
Android(java)学习笔记86:Android提供打开各种文件的API接口:setDataAndType
查看>>
javaweb学习总结(三十四)——使用JDBC处理MySQL大数据
查看>>
2019.6.7正式注册cnblogs
查看>>
windows phone 8 锁屏界面 显示应用程序的消息提醒
查看>>
P2605 [ZJOI2010]基站选址
查看>>
10月9日模拟题解题报告
查看>>