Normally you don't need to use ScrollBars directly because other controls such as ListBox, ScrollViewer, and TextBox provide them automatically, but you can use a ScrollBar to let the user select a value if you want. The WPF ScrollBar works much like the Windows Forms version does, providing relatively intuitive Minimum
, and Value
properties, and a ValueChanged
The ScrollViewer displays one child inside a scrolling window. The control is pleasantly easy to use—simply drop a child into it such as an Image with its Stretch
property set to None
Two important ScrollViewer properties are HorizontalScrollBarVisibility
, which determine when the control's scroll bars are visible. Their values can be Auto
, or Visible
. (For some strange reason, HorizontalScrollBarVisibility
defaults to Disabled and VerticalScrollBarVisibility
defaults to Visible
. You'll probably want to set both of these to Auto
The Separator places a separator in a menu or toolbar.
The Slider control is somewhat similar to the Windows Forms TrackBar control. Both allow the user to select a value from a sliding scale and provide Minimum
, and Value
properties. The following list describes the other most useful Slider properties.
- IsSnapToTickEnabled: Determines whether the control's value snaps to the nearest tick mark.
- IsSelectionRangeEnabled: Determines whether the user can select a range of values.
- TickFrequency: The spacing between tick marks.
- TickPlacement: The location of the tick marks. This can be BottomRight (bottom or right depending on the orientation), TopLeft (top or left), or Both (both top and bottom, or both left and right).
- Ticks: An array of positions to place tick marks.
The StackPanel is an extremely useful container that arranges its children closely packed in either a single row or a single column. For example, the following code shows how the ControlSamples
program places information inside its Expander. The StackPanel packs the three labels one after another vertically.
<Label Content="First Name: Rod"/>
<Label Content="Last Name: Stephens"/>
<Label Content="Occupation: Dilettante"/>
actually uses several StackPanels to make its columns of controls. It also uses StackPanels to arrange smaller groups within the columns, such as the horizontal groups holding the Polygon and Polyline, ScrollBar and Label, and Slider and Label.
StackPanel and Grid are probably the most-used WPF container controls.
The StatusBar displays an area (normally at the bottom of the form) where you can display status information. The StatusBar should contain StatusBarItems that hold the controls that you want to display.
A StatusBarItem can contain Labels, Buttons, Sliders, and even containers such as Grids that can hold other controls; however, you should generally keep the StatusBar contents simple.
The TabControl (yes, the word "Control" is part of its name) displays a set of tabs. When you click on a tab, the control displays the corresponding content.
The TabControl holds a set of TabItem objects. The TabItem's Header
property determines what is displayed in the tab. Each TabItem should contain a single child that holds whatever you want to display on the tab. Typically this child is a container such as a Grid or StackPanel that holds other controls.
The following code shows how ControlSamples
makes its TabControl.
<CheckBox Content="Orange Juice"/>
<CheckBox Content="Soda" IsChecked="True"/>
<CheckBox Content="Sandwich" IsChecked="True"/>
The TextBlock displays read-only text like a Label, but also supports additional features such as word wrapping, italics, bold, and colored text.
There's no room for a full discussion of the TextBlock here but thinking of it as a fancy Label will take you pretty far. You can use Run objects inside the control's contents to temporarily change the text's style.
The following code shows how the ControlSamples
program displays its TextBlock.
<TextBlock TextWrapping="Wrap" FontWeight="Normal">
The TextBlock can display
and otherwise formatted text.
The TextBox control is similar to its Windows Forms counterpart. One nice addition to the WPF version is an automatic spell checking feature, complete with a suggestions popup similar to the one provided by the RichTextBox.
A ToolBar displays a set of small buttons and other controls for quick access.
To give the user the full ToolBar experience, place one or more ToolBars inside a ToolBarTray. The ToolBarTray arranges the ToolBars and lets users drag them around. Place Buttons and whatever else you need inside the ToolBars.
If you have multiple ToolBars, you can set their Band
properties to indicate which horizontal band should initially hold them in the tray.
program shown in Figure 2
defines two ToolBars in bands 0
As you can probably guess, the ToolTip displays a tooltip. The easiest way to use a ToolTip is to set another control's ToolTip
property to the text that you want the ToolTip to display. For example, the following code sets a Button's ToolTip.
<Button ToolTip="Do something interesting with a cellphone.">
<Image Source="cellphone.ico" Width="24" Height="24"/>
If you want a ToolTip to do something more creative than simply displaying text, you can set a control's ToolTip property as an attribute element, and place a single child inside the ToolTip. If you like, the child can be a container that holds several controls.
The following code makes a Button's ToolTip use a TextBlock with word wrapping.
Do something interesting with a CD.
<Image Source="cdmusic.ico" Width="24" Height="24"/>