Controls


You can use hundreds of controls with WPF. For a better understanding, the controls are categorized into these groups:

  • Simple controls

  • Content controls

  • Headered content controls

  • Items controls

  • Headered items controls

Simple Controls

Simple controls are controls that don’t have a Content property. With the Button class, you’ve seen that the Button can contain any shape, any element you like. This is not possible with simple controls. In the following table, you can see simple controls and their functionality.

Open table as spreadsheet

Simple Control

Description

PasswordBox

The PasswordBox control is used to enter a password. This control has specific properties for password input; for example, PasswordChar to define the character that should show up as the user enters the password, or Password to access the password entered. The PasswordChanged event is invoked as soon as the password is changed.

ScrollBar

The ScrollBar control is a control containing a Thumb where the user can select a value. A scroll bar can be used, for example, if a document doesn’t fit on the screen. Some controls contain scroll bars that show up if the content is too big.

ProgressBar

With the ProgressBar control, you can indicate the progress of a lengthy operation.

Slider

With the Slider control the user can select a range of values by moving a Thumb. ScrollBar, ProgressBar, and Slider are derived from the same base class, RangeBase.

TextBox

The TextBox control is used to display simple unformatted text.

RichTextBox

The RichTextBox control supports rich text with the help of the FlowDocument class. RichTextBox and TextBox are derived from the same base class, TextBoxBase.

Tip 

Although simple controls do not have a Content property, you can completely customize the look of the control by defining a template. Templates are discussed later in this chapter.

Content Controls

A ContentControl has a Content property. With the Content property, you can add any content to the control. The Button class derives from the base class ContentControl, so you can add any content to this control. In a previous example, you saw a Canvas control within the Button. Content controls are described in the following table.

Open table as spreadsheet

ContentControl Controls

Description

Button

RepeatButton

ToggleButton

CheckBox

RadioButton

The classes Button, RepeatButton, ToggleButton, and GridViewColumnHeader are derived from the same base class, ButtonBase. All buttons react to the Click event. The RepeatButton raises the Click event repeatedly until the button is released.

ToggleButton is the base class for CheckBox and RadioButton. These buttons have an on and off state. The CheckBox can be selected and cleared by the user; the RadioButton can be selected by the user. Clearing the RadioButton must be done programmatically.

Label

The Label class represents the text label for a control. This class also has support for access keys; for example, a menu command.

Frame

The Frame control supports navigation. You can navigate to a page content with the Navigate() method. If the content is a Web page, a browser control is used for display.

ListBoxItem

ListBoxItem is an item inside a ListBox control.

StatusBarItem

StatusBarItem is an item inside a StatusBar control.

ScrollViewer

The ScrollViewer control is a content control that includes scroll bars. You can put any content in this control; the scroll bars will show up as needed.

ToolTip

ToolTip creates a pop-up Window to display additional information for a control.

UserControl

Using the class UserControl as a base class provides a simple way to create custom controls. However, the base class UserControl does not support templates.

Window

The Window class allows you to create windows and dialog boxes. With the Window class, you get a frame with minimize/maximize/ close buttons and a system menu. When showing a dialog box, you can use the method ShowDialog(); the method Show() opens a window.

NavigationWindow

The class NavigationWindow derives from the Window class and supports content navigation.

Only a Frame control is contained within the Window of the following XAML code. The Source property is set to http://www.wrox.com, so the Frame control navigates to this Web site, as you can see in Figure 31-9.

  <Window x:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="FrameSample" Height="400" Width="400">   <Frame Source="http://www.wrox.com" /> </Window> 

image from book
Figure 31-9

Headered Content Controls

Content controls with a header are derived from the base class HeaderedContentControl. HeaderedContentControl itself is derived from the base class ContentControl. The class HeaderedContentControl has a property Header to define the content of the header and HeaderTemplate for complete customization of the header. The controls that are derived from the base class HeaderedContentControl are listed in the following table.

Open table as spreadsheet

HeaderedContentControl

Description

Expander

With the Expander control, you can create an “advanced” mode with a dialog that, by default, does not show all information but that can be expanded by the user to show more information. In the unexpanded mode header information is shown; in expanded mode the content is visible.

GroupBox

The GroupBox control provides a border and a header to group controls.

TabItem

TabItem controls are items within the class TabControl. The Header property of the TabItem defines the content of the header shown with the tabs of the TabControl.

A simple use of the Expander control is shown in the next example. The Expander control has the property Header set to Click for more. This text is displayed for expansion. The content of this control is only shown if the control is expanded. Figure 31-10 shows the sample application with a collapsed Expander control. Figure 31-11 shows the same application with an expanded Expander control.

 <Window x:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Expander Sample" Height="300" Width="300">     <StackPanel>      <TextBlock>Short information</TextBlock>      <Expander Header="Click for more">          <TextBlock>More information here!</TextBlock>      </Expander>     </StackPanel> </Window>

image from book
Figure 31-10

image from book
Figure 31-11

Tip 

If you want the header text of the Expander control to change when the control is expanded, you can create a trigger. Triggers are explained later in this chapter.

Items Controls

The class ItemsControl contains a list of items that can be accessed with the Items property. Classes that are derived from ItemsControl are shown in the following table.

Open table as spreadsheet

ItemsControl

Description

Menu

ContextMenu

The classes Menu and ContextMenu are derived from the abstract base class MenuBase. You can offer menus to the user by placing MenuItem elements in the items list and associating commands.

StatusBar

The StatusBar control is usually shown at the bottom of an application to give status information to the user. You can put StatusBarItem elements inside a StatusBar list.

TreeView

For a hierarchical display of items, you can use the TreeView control.

ListBox

ComboBox

TabControl

ComboBox, ListBox, and TabControl have the same abstract base class, Selector. This base class makes it possible to select items from a list. The ListBox displays the items from a list. The ComboBox has an additional Button control to display the items only if the button is clicked. With the TabControl, content can be arranged in tabular form.

Headered Items Controls

HeaderedItemsControl is the base class of controls that include items but also has a header. The class HeaderedItemsControl is derived from ItemsControl.

Classes that are derived from HeaderedItemsControl are listed in the following table.

Open table as spreadsheet

HeaderedItemsControl

Description

MenuItem

The menu classes Menu and ContextMenu include items of type MenuItem. Menu items can be connected to commands, as the MenuItem class implements the interface ICommandSource.

TreeViewItem

The TreeView class can include items of type TreeViewItem.

ToolBar

The ToolBar control is a container for a group of controls, usually Button and Separator elements. You can place the ToolBar inside a ToolBarTray that handles rearranging of ToolBar controls.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net