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.

Properties
Password
- Sets or returns the passwordPasswordChar
- 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 theTextBox
, 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.

Properties
IsDocumentEnabled
- Allows accessing controls used in theRichTextBox
(e.g. aButton
, 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).

Properties
MaxLength
- Maximal allowed length of the inserted text.IsReadOnly
- Restricts editing of the control's value.IsUndoEnabled
- Enables the "Undo" functionSpellCheck.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 theTextBox
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.

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.

Properties
Foreground
- The color of the bar representing the status.IsIndeterminate
- If set totrue
, only a continuously running stripe is displayed. If set tofalse
, 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;

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 termsDownloaded 351x (564.07 kB)