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

 

posted on 2014-02-24 00:13  大Z  阅读(563)  评论(0编辑  收藏  举报