Helper class to work with a Status Bar in WPF.

We need lot of functionalites to be performed on the Status bar. Here is a helper class that abstracts the common functionality. The code snippets on how to use these are provided at the end.

using System.Collections.Generic;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;

namespace Utilities
{
    /// <summary>
    /// StatusBar Helper.
    /// </summary>
    public class StatusBarHelper
    {
         private static ProgressBar progressBar;
        private static ToolTip ttprogbar;

        /// <summary>
        /// Gets the text block.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static StatusBarItem GetTextBlock(string text)
        {
             if (string.IsNullOrEmpty(text))
                 throw new ArgumentNullException("text");

            StatusBarItem statusBarItem = new StatusBarItem();

            TextBlock textBlock = new TextBlock();
            textBlock.Text = text;
            statusBarItem.Content = textBlock;

             return statusBarItem;
        }

        //TODO: Remove the following method, not required anymore.
        /// <summary>
        /// Gets the progress bar status item.
        /// </summary>
        /// <param name="toolTipText">The tool tip text.</param>
        /// <returns></returns>
        public static StatusBarItem GetProgressBarStatusItem(string toolTipText)
        {
            StatusBarItem statusBarItem = new StatusBarItem();

            ProgressBar progressBar = new ProgressBar();
            progressBar.Width = 150;
            progressBar.Height = 13;
            Duration duration = new Duration(TimeSpan.FromSeconds(1));
            DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
            doubleanimation.RepeatBehavior = RepeatBehavior.Forever;
            progressBar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);

             if (string.IsNullOrEmpty(toolTipText))
            {
                ToolTip ttprogbar = new ToolTip();
                ttprogbar.Content = toolTipText;
                progressBar.ToolTip = (ttprogbar);
            }

            statusBarItem.Content = progressBar;
            statusBarItem.HorizontalAlignment = HorizontalAlignment.Right;

             return statusBarItem;
        }

        /// <summary>
        /// Gets the progress bar.
        /// </summary>
        /// <param name="toolTipText">The tool tip text.</param>
        /// <returns></returns>
        public static ProgressBar GetProgressBar(string toolTipText)
        {
             if (progressBar == null)
            {
                progressBar = new ProgressBar();

                progressBar.Width = 150;
                progressBar.Height = 13;
                Duration duration = new Duration(TimeSpan.FromSeconds(1));
                DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
                doubleanimation.RepeatBehavior = RepeatBehavior.Forever;
                progressBar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
             }

             if (ttprogbar == null)
            {
                ttprogbar = new ToolTip();
                progressBar.ToolTip = (ttprogbar);
             }

             if (string.IsNullOrEmpty(toolTipText))
            {
                ttprogbar.Content = toolTipText;
             }

             return progressBar;
        }

        /// <summary>
        /// Gets the separator.
        /// </summary>
        /// <param name="style">The style.</param>
        /// <returns></returns>
        public static StatusBarItem GetSeparator(Style style)
        {
            StatusBarItem statusBarItem = new StatusBarItem();

            Separator sp          = new Separator();
            sp.Style              = style;
            statusBarItem.Content = sp;
             return statusBarItem;
        }


    }
}

//To add an status bar item with a message;
string message = "test message";
MainStatusBar.Items.Add(StatusBarHelper.GetTextBlock(message));


//To add a separator
Style style = FindResource("StatusBarSeparatorStyle") as Style;
if (style != null)
    MainStatusBar.Items.Add(StatusBarHelper.GetSeparator(style));
            
//To add a Progress Bar with a message.
MainStatusBar.Items.Add(StatusBarHelper.GetProgressBar(message));
MainStatusBar.Items.Content = StatusBarHelper.GetProgressBar(message);




Cheers,
Diablo III
By [)ia6l0 iii   Popularity  (2190 Views)