WPF StatusBar Control


As WPF Tutorial says: “With the top of the application window usually occupied by the main menu and/or toolbars, described in previous chapters, the bottom part of the window is usually the home of the status bar. The status bar is used to show various information about the current state of the application, like cursor position, word count, progress of tasks and so on.” They go on later to say “the real advantage of the StatusBar comes when we need to divide it into several areas of information.”

Below is an example from WPF Tutorial. This project only has one StatusBarItem, a TextBlock.

Below is the XAML.

<Window x:Class="StatusBarSimple.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StatusBarSimple"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="StatusBarSimple" Height="250" Width="400">
    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" Background="Azure">
            <StatusBarItem>
                <TextBlock Name="lblCursorPosition" />
            </StatusBarItem>
        </StatusBar>
        <TextBox AcceptsReturn="True" Name="txtEditor" SelectionChanged="txtEditor_SelectionChanged" />
    </DockPanel>
</Window>

A More Advanced (yet Incomplette) StatusBar

Below is the XAML code.

<Window x:Class="StatusBar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StatusBar"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="StatusBar" Height="250" Width="400" MinWidth="350">
    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="340" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem>
                <TextBlock Name="lblCursorPosition" />
            </StatusBarItem>
            <Separator Grid.Column="1" />
            <StatusBarItem Grid.Column="2">
                <TextBlock Text="c:\path\of\current\file.txt" />
            </StatusBarItem>
            <Separator Grid.Column="3" />
            <StatusBarItem Grid.Column="4">
                <ProgressBar Value="50" Width="90" Height="16" />
            </StatusBarItem>
        </StatusBar>
        <TextBox AcceptsReturn="True" Name="txtEditor" SelectionChanged="txtEditor_SelectionChanged" />
    </DockPanel>
</Window>
using System.Windows;
namespace StatusBar
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void txtEditor_SelectionChanged(object sender, RoutedEventArgs e)
        {
            string str = txtEditor.Text;
            int row = txtEditor.GetLineIndexFromCharacterIndex(txtEditor.CaretIndex);
            int col = txtEditor.CaretIndex - txtEditor.GetCharacterIndexFromLineIndex(row);
            int sel = txtEditor.SelectionLength;
            int len = txtEditor.Text.Length;  // includes CR LF
            int lines = txtEditor.LineCount;
            int chars = len - (2 * (lines - 1));  // remove CR LF

            lblCursorPosition.Text = "Chars " + chars + " , Len " + len + " , Lines " + 
                                        lines + " , Line " + (row + 1) +
                                        ", Char " + (col + 1) + " , Sel " + sel ;
        }
    }
}

A Bit More Advanced

<Window x:Class="StatusBar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StatusBar"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="StatusBar" Height="250" Width="400" MinWidth="350">
    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="340" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem>
                <TextBlock Name="lblCursorPosition" />
            </StatusBarItem>
            <Separator Grid.Column="1" />
            <StatusBarItem Grid.Column="2">
                <TextBlock Text="c:\path\of\current\file.txt" />
            </StatusBarItem>
            <Separator Grid.Column="3" />
            <StatusBarItem Grid.Column="4">
                <ProgressBar Value="50" Width="90" Height="16" />
            </StatusBarItem>
        </StatusBar>
        <TextBox AcceptsReturn="True" Name="txtEditor" SelectionChanged="txtEditor_SelectionChanged" />
    </DockPanel>
</Window>
using System.Windows;
namespace StatusBar
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void txtEditor_SelectionChanged(object sender, RoutedEventArgs e)
        {
            string str = txtEditor.Text;
            int row = txtEditor.GetLineIndexFromCharacterIndex(txtEditor.CaretIndex);
            int col = txtEditor.CaretIndex - txtEditor.GetCharacterIndexFromLineIndex(row);
            int sel = txtEditor.SelectionLength;
            int len = txtEditor.Text.Length;  // includes CR LF
            int lines = txtEditor.LineCount;
            int chars = len - (2 * (lines - 1));  // remove CR LF

            lblCursorPosition.Text = "Chars " + chars + " , Len " + len + " , Lines " + 
                                        lines + " , Line " + (row + 1) +
                                        ", Char " + (col + 1) + " , Sel " + sel ;
        }
    }
}