Get up to 80 % extra points for free! More info:

Lesson 36 - WPF - Data Storage and Descriptive Controls

In the previous lesson, WPF - Data Input Controls, we begun our overview of data input controls. In today's C# .NET WPF tutorial, we're going to complete the overview, and also look at descriptive controls.

PasswordBox

This control is used for entering passwords.

PasswordBox in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • Password - Sets or returns the password
  • PasswordChar - Specifies the character to replace entered text's characters (so that the entered password can't be seen)

Events

  • PasswordChanged - Triggered every time the entered value is changed. As for the TextBox, the method shouldn't take too long to execute since it's called every time a new character is typed in.

Example

XAML

<PasswordBox x:Name="psbxPassword" Width="150"
    BorderBrush="Black" BorderThickness="2,2,1,1"
    HorizontalAlignment="Left" VerticalContentAlignment="Center"
    PasswordChanged="ShowPassword" MaxLength="8"/>

C#

private void ShowPassword(object sender, RoutedEventArgs e)
{
    lblPassword.Content = psbxPassword.Password.ToString();
}

RichTextBox (Formatted TextBox)

We use this element to insert or display larger and formatted text.

RichTextBox in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • IsDocumentEnabled - Allows accessing controls used in the RichTextBox (e.g. a Button, etc.).
  • IsReadOnly - Sets the control to read-only.
  • IsUndoEnabled - Enables the "Undo" feature.
  • SpellCheck.IsEnabled - Enables the spell check (true / false).

Events

No important events.

Example

XAML

<RichTextBox x:Name="rtbText"
    BorderBrush="Black" BorderThickness="2,2,1,1"
    VerticalScrollBarVisibility="Auto"/>

TextBox

A control used for entering text values or even values in other formats (e.g. numbers).

TextBox in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • MaxLength - Maximal allowed length of the inserted text.
  • IsReadOnly - Restricts editing of the control's value.
  • IsUndoEnabled - Enables the "Undo" function
  • SpellCheck.IsEnabled - Enables the spell check (true / false).
  • Text - The displayed text.

Events

  • GotFocus - The assigned method is called when the control is focused (e.g. by clicking in it). This can be used, for example, to clear the TextBox before entering new values.
  • TextChanged - Calls the assigned method every time we insert a character into the control. This can be used, for example, to refresh other controls or labels. It's recommended that we write short methods for this event, because, as mentioned earlier, it's triggered whenever any character is typed in.

Example

XAML

<TextBox x:Name="tbxCount" Width="60" Height="30"
    Background="LightGray" BorderBrush="Black" BorderThickness="2,2,1,1"
    MaxLength="5" HorizontalAlignment="Right" Padding="0,0,5,0"
    HorizontalContentAlignment="Right" VerticalContentAlignment="Center"
    GotFocus="ClearControl" PreviewTextInput="InputCheck"/>

C#

The GotFocus event:

// Clears the contents of the textbox after clicking in it
private void ClearControl(object sender, RoutedEventArgs e)
{
    tbxPocet.Text = "";
}

The PreviewTextInput event:

using System.Text.RegularExpressions;
// ...

// Only numbers, dots, and commas are allowed by the regular expression
private void InputCheck(object sender, TextCompositionEventArgs e)
{
    Regex regex = new Regex("\\d|[,.]");
    e.Handled = !regex.IsMatch(e.Text);
}

Descriptive Controls

Next, we'll describe WPF controls that are used as labels for other controls. Those are:

  • Label
  • ProgressBar
  • TextBlock

Label

Is used for describing other controls. Unlike the TextBlock below, it allows us to set borders of the control, display images (or other internal content), or set focus.

Label in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • Content - The text being displayed.

Events

No important events.

Example

XAML

<Label Content="Sample text"
    FontFamily="Tahoma" FontSize="14" FontWeight="Bold"
    Foreground="Blue"/>

ProgressBar

We use this control to show the status of some process. For example, when reading data in a loop, it can show how much has already been read.

ProgressBar in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • Foreground - The color of the bar representing the status.
  • IsIndeterminate - If set to true, only a continuously running stripe is displayed. If set to false, the current value is displayed.
  • LargeChange - The value by which the total value is increased or decreased when the indicator is clicked.
  • Maximum - The maximum value.
  • Minimum - The minimum value.
  • Orientation - The orientation of the control (horizontal or vertical).
  • SmallChange - The value by which the total value increased or decreased when the arrow is clicked.
  • Value - The current value.

Events

No important events.

Example

XAML

<ProgressBar x:Name="pbrPointer" Height="10"
    BorderBrush="Black" BorderThickness="2,2,1,1"
    Foreground="Lime" Maximum="10" />

TextBlock

This control allows us to display larger, multi-line text. Unlike the Label, however, it only displays text;

TextBlock in C# .NET WPF - Form Applications in C# .NET WPF

Properties

  • TextWrapping - Specifies the text wrapping.

Events

No important events.

Example

XAML

<TextBlock x:Name="tbkText" Background="White"
    TextWrapping="Wrap" Padding="10"/>

 

Download

By downloading the following file, you agree to the license terms

Downloaded 304x (564.07 kB)

 

Previous article
WPF - Data Input Controls
All articles in this section
Form Applications in C# .NET WPF
Skip article
(not recommended)
WPF - Data Displaying Controls
Article has been written for you by Tomáš Bitter
Avatar
User rating:
No one has rated this quite yet, be the first one!
Activities