Fork me on GitHub

ProgressBar

从XAML文件中可以看出,一组BadioButton按钮,用来变化ProgressBar的样式。ProgressBar是包含在StatusBar中的。
XAML文件:
<Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
</Grid.RowDefinitions>
    <TextBlock Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" Grid.Row="0" FontSize="20">Progress Bars</TextBlock>
    <StackPanel Grid.Column="0" Margin="10,30.6,-10,0" VerticalAlignment="Top" Height="124.8">
      <Label>Choose number of ProgressBar iterations.</Label>
      <RadioButton Content="One" Click="MakeOne"/>
      <RadioButton Content="Three" Click="MakeThree"/>
      <RadioButton Content="Five" Click="MakeFive"/>
      <RadioButton Content="Forever" Click="MakeForever"/>
      <RadioButton Content="Indeterminate" Click="MakeIndeterminate"/>
    </StackPanel>
    <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>
  </Grid>
下面来一个一个分析,首先sbar.Items.Clear();来清空“sbar”中的所有子项,下来新建一个Label,并设置它的颜色和设置的内容,并把它添加到“sbar”中。ProgressBar progbar = new ProgressBar();动态创建一个进度条。           progbar.IsIndeterminate是来设置进度条是从头到尾进行,还是一次次滚动,如果为false,则是从头到尾,为true则是一次次循环滚动,默认值为false。Duration duration = new Duration(TimeSpan.FromSeconds(10));进行完一次所需的时间。DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);定了进度条的进行到的值。doubleanimation.RepeatBehavior = new RepeatBehavior(3);是进度条重复的次数,默认情况下是一次。RepeatBehavior.Forever ;则是无限时进行。progbar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);把doubleanimation加载到progbar中。其中在MakeFive过程中,实现了把一个图片处理成ImageBrush的过程。
CS文件:
private void MakeOne(object sender, RoutedEventArgs e)
        {
           sbar.Items.Clear();
           Label lbl = new Label();
           lbl.Background = new LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90);
           lbl.Content = "ProgressBar with one iteration.";
           sbar.Items.Add(lbl);
           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);
          }

         private void MakeThree(object sender, RoutedEventArgs e)
         {
           sbar.Items.Clear();
           Label lbl = new Label();
           lbl.Background = new LinearGradientBrush(Colors.Pink, Colors.Red, 90);
           lbl.Content = "ProgressBar with three iterations.";
           sbar.Items.Add(lbl);
           ProgressBar progbar = new ProgressBar();
           progbar.Background = Brushes.Gray;
           progbar.Foreground = Brushes.Red;
           progbar.Width = 150;
           progbar.Height = 15;
           Duration duration = new Duration(TimeSpan.FromMilliseconds(2000));
           DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
           doubleanimation.RepeatBehavior = new RepeatBehavior(3);
           progbar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
           sbar.Items.Add(progbar);
         }
       
        private void MakeFive(object sender, RoutedEventArgs e)
        {
            sbar.Items.Clear();
            TextBlock txtb = new TextBlock();
            txtb.Text = "ProgressBar with five iterations.";
            sbar.Items.Add(txtb);
            Image image = new Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/data/e.jpg");
            bi.EndInit();
            image.Source = bi;
            ImageBrush imagebrush = new ImageBrush(bi);
            ProgressBar progbar = new ProgressBar();
            progbar.Background = imagebrush;
            progbar.Width = 150;
            progbar.Height = 15;
            Duration duration = new Duration(TimeSpan.FromMilliseconds(2000));
            DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
            doubleanimation.RepeatBehavior = new RepeatBehavior(5);
            progbar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
            sbar.Items.Add(progbar);
         }
 
         private void MakeForever(object sender, RoutedEventArgs e)
         {
           sbar.Items.Clear();
           Label lbl = new Label();
           lbl.Background = new LinearGradientBrush(Colors.LightBlue,       
                                                    Colors.SlateBlue, 90);
           lbl.Content = "ProgressBar with infinite iterations.";
           sbar.Items.Add(lbl);
           ProgressBar progbar = new ProgressBar();
           progbar.Width = 150;
           progbar.Height = 15;
           Duration duration = new Duration(TimeSpan.FromSeconds(1));
           DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
           doubleanimation.RepeatBehavior = RepeatBehavior.Forever;
           progbar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
           sbar.Items.Add(progbar);
         
         }

         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.Red;
             progbar.Width = 150;
             progbar.Height = 15;
             progbar.IsIndeterminate = true;
             sbar.Items.Add(progbar);
         }
效果如下图:


本文参考MSDN组织

posted @ 2007-04-23 10:40  桂素伟  阅读(4609)  评论(0编辑  收藏  举报