WPF FAQ - StatusBar

Find answers for the most frequently asked questions
Expand All Collapse All

You cannot place controls into a StatusBar control in the Designer. However, you can add any number of controls to the StatusBar programmatically through it’s ‘Controls’ property. After adding the controls, set their Visible, Location, Bounds and other properties.

Here’s a sample method that could be called in a form’s constructor after the ’InitializeComponent’.

[C#]

 private void AddStatusBarControls(StatusBar sb)
        {
            ProgressBar pb = new ProgressBar();
            sb.Controls.Add(pb);

            pb.Visible = true;
            pb.Bounds = new Rectangle(sb.Width / 4, 4, sb.Width / 2, sb.Height - 8);
            pb.Anchor = AnchorStyles.Left | AnchorStyles.Right;
        }

Permalink

StatusBar uses a DockPanel as an item template and docks all the items in the control to the left except for the last item. The last item takes the rest of the space in the statusbar. To have a custom layout for the statusbar control, you can change the DockPanel to a StackPanel, WrapPanel etc. You can also align the items in the statusbar using the DockPanel.Dock property for each statusbar item.

The following lines of code are used to change the alignment of the statusbar items.

[XAML]

<StatusBar Height='30' VerticalAlignment='Bottom' Background='Azure'>
<StatusBarItem DockPanel.Dock='Right'>
      	<TextBlock Text='Welcome to WPF!!'/>
</StatusBarItem>
      <StatusBarItem>
      	<TextBlock Text='Hello World! '/>
</StatusBarItem>
</StatusBar>
Permalink
[C#]

private void statusBar1_PanelClick(object sender, 
System.Windows.Forms.StatusBarPanelClickEventArgs e)
{
   switch (statusBar1.Panels.IndexOf(e.StatusBarPanel))
   {
      case 0 :
         MessageBox.Show('You have clicked Panel One.');
         break;
      case 1 :
         MessageBox.Show('You have clicked Panel Two.');
         break;
   }
}
//Place the following code in the form’s constructor to register the event handler.

   this.statusBar1.PanelClick += new 
   System.Windows.Forms.StatusBarPanelClickEventHandler 
   (this.statusBar1_PanelClick);

Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.