Fork me on GitHub

StatusBar

 

本例子的XAML代码中定义了一个Separator的样式资源,名称为StatusBarSeparatorStyle,另外定议了一个List,用来展示不用的StatusBar

XAML代码:

<Window.Resources>

    <Style x:Key="StatusBarSeparatorStyle" TargetType="Separator">

      <Setter Property="Background" Value="LightBlue" />

      <Setter Property="Control.Width" Value="10"/>

      <Setter Property="Control.Height" Value="20"/>

    </Style>   

 </Window.Resources>

<Grid>

 <Grid.ColumnDefinitions>

    <ColumnDefinition/>

    <ColumnDefinition/>

 </Grid.ColumnDefinitions>

 <Grid.RowDefinitions>

    <RowDefinition/>

    <RowDefinition/>

    <RowDefinition/>

 </Grid.RowDefinitions>

 <StackPanel Grid.Column="0" Grid.Row="0">

    <ListBox HorizontalAlignment="Left" VerticalAlignment="Top"

          Width="150" Margin="0,10,0,0" Foreground="Blue">

        <ListBoxItem Content="Download Application" Selected="MakeProgressBar"/>

        <Separator/>

        <ListBoxItem Content="Compilation Application" Selected="MakeTextBlock"/>

        <Separator/>

        <ListBoxItem Content="Printing Application" Selected="MakeImage"/>

        <Separator/>

        <ListBoxItem Content="Help Application" Selected="MakeHelp"/>

        <Separator/>

        <ListBoxItem Content="Icon Group" Selected="MakeGroup"/>

    </ListBox>

 </StackPanel>

 

<StatusBar Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"

           VerticalAlignment="Bottom" Background="Beige" >

     <StatusBarItem>

       <TextBlock>Ready</TextBlock>

     </StatusBarItem>

     <StatusBarItem Height="26">

       <Separator Style="{StaticResource StatusBarSeparatorStyle}" RenderTransformOrigin="0.5,0.5" Margin="0,2,0,0" Width="22.729" Height="4.056">

                  <Separator.RenderTransform>

                              <TransformGroup>

                                          <ScaleTransform ScaleX="1" ScaleY="1"/>

                                          <SkewTransform AngleX="0" AngleY="0"/>

                                          <RotateTransform Angle="90.114"/>

                                          <TranslateTransform X="0" Y="0"/>

                              </TransformGroup>

                  </Separator.RenderTransform>

       </Separator>

     </StatusBarItem>

</StatusBar>

</Grid>

下面着重说一下CS中的代码。

首先,每个过程中都首先清空原来的StatusBar,及sbar.Items.Clear();

MakeProgressBar首先创建了一个TextBlock添加到StatusBar中,接下来创建了一个ProgressBar同样添加到StatusBar中,这样,一个StatusBar中有两个子项(Items),一个是用来显示内容的TextBlock,另一个是进度条(关于进度条的属性在ProgressBar中有详细介绍)。

MakeTextBlock是创建了一个TextBlock和一个Rectangle

MakeImage是创建了一个TextBlock和一个引用外部图片的Image,且位于StatusBar的右边。

MakeHelp是创建了一个TextBlock和一个引用外部图片的Image,且位于StatusBar的左边。

MakeGroup则创建两个图片,添加入StatusBar中。

相对传统的StatusBarWPF中的StatusBar有更丰富的表现形式,正如WPF中的控件都有嵌套的功能。

CS代码:

   private void MakeProgressBar(object sender, RoutedEventArgs e)

         {

           sbar.Items.Clear();

           TextBlock txtb = new TextBlock();

           txtb.Text = "Progress of download.";

           sbar.Items.Add(txtb);

 

           ProgressBar progressbar = new ProgressBar();

 

           progressbar.IsIndeterminate = false;

           progressbar.Width = 150;

           progressbar.Height = 15;

           progressbar.Orientation = Orientation.Horizontal;

           Duration duration = new Duration(TimeSpan.FromSeconds(10));

           DoubleAnimation doubleanimation =new DoubleAnimation(100.0, duration);

           progressbar.BeginAnimation(ProgressBar.ValueProperty,doubleanimation);

           ToolTip ttprogbar = new ToolTip();

           ttprogbar.Content = "Shows the progress of a download.";

           progressbar.ToolTip = (ttprogbar);

           sbar.Items.Add(progressbar);

 

         }

 

         private void MakeTextBlock(object sender, RoutedEventArgs e)

         {

           sbar.Items.Clear();

           TextBlock txtb = new TextBlock();

           txtb.Text = "Code compiled successfully.";

           sbar.Items.Add(txtb);

           Rectangle rect = new Rectangle();

           rect.Height = 20;

           rect.Width = 1;

           rect.Fill = Brushes.LightGray;

           sbar.Items.Add(rect);

         }

 

         private void MakeImage(object sender, RoutedEventArgs e)

         {

          sbar.Items.Clear();

          DockPanel dpanel = new DockPanel();

          TextBlock txtb = new TextBlock();

          txtb.Text = "Printing ";

          dpanel.Children.Add(txtb);

          Image printImage = new Image();

          printImage.Width = 16;

          printImage.Height = 16;

          BitmapImage bi = new BitmapImage();

          bi.BeginInit();

         bi.UriSource = new Uri(@"pack://application:,,,/images/print.bmp");

          bi.EndInit();

          printImage.Source = bi;

          dpanel.Children.Add(printImage);

          TextBlock txtb2 = new TextBlock();

          txtb2.Text = " 5pgs";

          dpanel.Children.Add(txtb2);

          StatusBarItem sbi = new StatusBarItem();

          sbi.Content = dpanel;

          sbi.HorizontalAlignment = HorizontalAlignment.Right;

          ToolTip ttp = new ToolTip();

          ttp.Content = "Sent to printer.";

          sbi.ToolTip = (ttp);

          sbar.Items.Add(sbi);

         }

 

         private void MakeHelp(object sender, RoutedEventArgs e)

         {

          sbar.Items.Clear();

          Image helpImage = new Image();

          helpImage.Width = 16;

          helpImage.Height = 16;

          BitmapImage bi = new BitmapImage();

          bi.BeginInit();

          bi.UriSource = new Uri(@"pack://application:,,,/images/help.bmp");

          bi.EndInit();

          helpImage.Source = bi;

          ToolTip ttp = new ToolTip();

          ttp.Content = "HELP";

          helpImage.ToolTip = (ttp);

          sbar.Items.Add(helpImage);

         }

 

        private void MakeGroup(object sender, RoutedEventArgs e)

        {

             sbar.Items.Clear();

           Image helpImage = new Image();

            helpImage.Width = 16;

            helpImage.Height = 16;

            BitmapImage bi = new BitmapImage();

            bi.BeginInit();

            bi.UriSource = new Uri(@"pack://application:,,,/images/help.bmp");

            bi.EndInit();

            helpImage.Source = bi;

            ToolTip ttp = new ToolTip();

            ttp.Content = "HELP";

            helpImage.ToolTip = (ttp);

            sbar.Items.Add(helpImage);

 

            Separator sp = new Separator();

            sp.Style = (Style)FindResource("StatusBarSeparatorStyle");

            sbar.Items.Add(sp);

           

            Image printImage = new Image();

            printImage.Width = 16;

            printImage.Height = 16;

            BitmapImage bi_print = new BitmapImage();

            bi_print.BeginInit();

            bi_print.UriSource = new Uri(@"pack://application:,,,/images/print.bmp");

            bi_print.EndInit();

            printImage.Source = bi_print;

            ToolTip ttp_print = new ToolTip();

            ttp.Content = "Sent to printer.";

            printImage.ToolTip = (ttp_print);

            sbar.Items.Add(printImage);

        }

posted @ 2007-05-07 13:49  桂素伟  阅读(6037)  评论(0编辑  收藏  举报