WPF 进度条

//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);

//
<ProgressBarMinimum="0"Maximum="100"Name="pbStatus"/>

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

<ProgressBarMinimum="0"Maximum="100"Name="pbStatus"IsIndeterminate="True"/>

ProgressBar with text:

<GridMargin="20">
       
<ProgressBarMinimum="0"Maximum="100"Value="75"Name="pbStatus"/>
       
<TextBlockText="{Binding ElementName=pbStatus, Path=Value, StringFormat={}{0:0}%}"HorizontalAlignment="Center"VerticalAlignment="Center"/>
   
</Grid>

<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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值