What you willlearninthis chapter
Content controls. These controls can contain nested elements, giving them nearly
unlimited display abilities. They include the Label, Button, and ToolTip classes.
Headeredcontent controls. These are content controls that allow you to add a
main section of content and a separate title portion. They are usually intended to
wrap larger blocks of user interface. They include the TabItem, GroupBox, and
Expander classes.
Text controls. This is the small set of controls that allow users to enter input. The text
controls support ordinary text (the TextBox), passwords (the PasswordBox).
List controls. These controls show collections of items in a list. They include the
ListBoxand ComboBoxclasses. Range-based controls. These controls have just one
thing in common: a Value property that can be set to any number in a prescribed
range. Examples include the Slider and ProgressBarclasses.
Date controls. This category includes two controls that allow users to select dates:
the Calendar and DatePicker.
What you willlearninthis chapter
ListView. The ListViewderives from the plain-vanilla ListBox. It adds support for
column-based display and the ability to switch quickly between different “views,”
or display modes, without requiring you to rebind the data and rebuild the list.
TreeView. The TreeViewis a hierarchical container, which means you can create a
multilayereddata display. For example, you could create a TreeViewthat shows
category groups in its first level and shows the related products under each
category node.
DataGrid. The DataGridis WPF’s most full-featured data display tool. It divides your
data into a grid of columns and rows, like the ListView, but has additional
formatting features (such as the ability to freeze columns and style individual rows),
and it supports in-place data editing.
What you willlearninthis chapter
Menus. They’re one of the oldest user interface controls, and they’ve changed
surprisingly little in the past two decades. WPF includes solid, straightforward
support for main menus and popup context menus.
Toolbars and status bars. They decorate the top and bottom of countless
applications—sometimes when they aren’t even needed. WPF supports both
controls with its customary flexibility, allowing you to insert virtually any control
inside. However, the WPF toolbars and status bars don’t have many frills. They
support overflow menus, but they don’t provide floating and docking capability.
Ribbons. With only a little more effort, you can add an Office-style ribbon to the
top of your application window. It requires a separate (free) download, but you’ll
get some valuable built-in features, such as configurable resizing. You’ll also get an
Office-style menu feature to match.
TheListViewisaspecializedlistclassthat’sdesignedfordisplayingdifferentviews
ofthesamedata.TheListViewisparticularlyusefulifyouneedtobuildamulticolumn
viewthatdisplaysseveralpiecesofinformationabouteachdataitem.
TheListViewderivesfromtheListBoxclassandextendsitwithasingledetail:the
Viewproperty.TheViewpropertyisyetanotherextensibilitypointforcreatingrichlist
displays.Ifyoudon’tsettheViewproperty,theListViewbehavesjustlikeitslesser-
poweredancestor,theListBox.
ViewpropertypointstoaninstanceofanyclassthatderivesfromViewBase
(whichisanabstractclass).TheViewBaseclassissurprisinglysimple;infact,it’slittle
morethanapackagethatbindstogethertwostyles.OnestyleappliestotheListView
control(andisreferencedbytheDefaultStyleKeyproperty),andtheotherstyle
appliestotheitemsintheListView(and isreferenced by
theZItemContainerDefaultStyleKey property). The DefaultStyleKey and
ItemContainerDefaultStyleKeypropertiesdon’tactuallyprovidethestyle;instead,
theyreturnaResourceKeyobjectthatpointstoit.
THE LISTVIEW
MenuItems
MenusarecomposedofMenuItem
objectsandSeparatorobjects.The
MenuItem classderivesfrom
HeaderedItemsControl, because
eachmenuitemhasaheader(which
containsthetextforthatitem)and
canholdacollectionofMenuItem
objects(whichrepresentsasubmenu).
TheSeparatorsimplydisplaysa
horizontallineseparatingmenuitems.
Here’s a straightforward
combinationofMenuItemobjectsthat
createstherudimentarymenu
structureshowninFigure25-3:
MENUS
Setting menu item in
XAML
MenuItems
MENUS
Setting menu item in
Properties
Toolbarsandstatusbarsaretwowell-wornstaplesofthe
Windowsworld.Botharespecializedcontainersthatholda
collectionofitems.Traditionally,atoolbarholdsbuttons,anda
statusbarconsistsprimarilyoftextandothernoninteractive
indicators(likeaprogressbar).However,bothtoolbarsandstatus
barsareusedwithavarietyofdifferentcontrols.
InWindowsForms,toolbarsandstatusbarshavetheirown
contentmodel.Althoughit’sstillpossibletoplacearbitrarycontrols
insideatoolbarandstatusbarusingawrapper,theprocessisn’t
seamless.TheWPFtoolbarandstatusbardon’thavethislimitation.
TheysupporttheWPFcontentmodel,allowingyoutoaddany
elementtoatoolbarorstatusbarandgivingyouunparalleled
flexibility.
TOOLBARS AND STATUS BARS
TheToolbar
AtypicalWPFToolBarisfilledwithButton,ComboBox,CheckBox,RadioButton,and
Separatorobjects.Becausetheseelementsareallcontentcontrols(exceptforthe
Separator),youcanplacetextandimagecontentinside.Althoughyoucanuse
otherelements,suchasLabelandImagetoputnoninteractiveelementsintothe
ToolBar,theeffectisoftenconfusing.
Todemonstratewhatthislookslike,considerthesimplemarkupshownhere:
TOOLBARS AND STATUS BARS
Figure 25-5 shows this toolbar in action, with two CheckBox
controls in the checked state and the drop-down list on
display.
TheToolbar
TodemonstrateinDesignershownhere:
TOOLBARS AND STATUS BARS
TheToolbarTray
TheToolBarTraymakesiteasierfortoolbarstosharethesame
row,orband.YoucanconfiguretheToolBarTraysothattoolbars
shareaband,whileothersareplacedonotherbands.The
ToolBarTrayprovidestheshadedbackgroundbehindtheentire
ToolBararea.Butmostimportant,theToolBarTrayaddssupportfor
toolbardrag-and-dropfunctionality.Unlessyousetthe
ToolBarTray.IsLockedpropertytotrue,theusercanrearrangeyour
toolbarsinaToolBartraybyclickingthegripattheleftside.
Toolbarscanberepositionedinthesamebandormovedtoa
differentband.
TOOLBARS AND STATUS BARS
TheToolbarTray
Here’ssomesamplemarkupthatcreatesseveraltoolbarsinaToolBarTray.
Figure25-7showstheresult
TOOLBARS AND STATUS BARS
TheToolbarTray
Here’ssomesampledesignerthatcreatesseveraltoolbarsinaToolBarTray.Figure25-7
showstheresult
TOOLBARS AND STATUS BARS
TheStatusbar
TheStatusBardoesn’tworkverywellifyouwanttouseoneof
theButtonBase-derivedelementsortheComboBox.Itdoesn’t
overridethestylesofanyofthesecontrols,sotheylookoutof
placeinthestatusbar.Ifyouneedtocreateastatusbarthat
includesthesecontrols,youmightconsiderdockinganordinary
ToolBarcontroltothebottomofyourwindow.It’sprobablyasa
resultofthisgenerallackoffeaturesthattheStatusBarisfoundin
theSystem.Windows.Controls.Primitivesnamespaceratherthanin
themoremainstreamSystem.Windows.Controlsnamespacewhere
theToolBarcontrolexists.
TOOLBARS AND STATUS BARS
TheStatusbar
Onewaytogetproportionallyorright-aligneditemsistouseaGridforyourlayout
container.TheonlytrickisthatyoumustwrapthechildelementinaStatusBarItem
objectinordertosettheGrid.Columnpropertyappropriately.Here’sanexamplethat
usesaGridtoplaceoneTextBlockontheleftsideofaStatusBarandanotheronthe
rightside:
TOOLBARS AND STATUS BARS
TheStatusbar
Settingindesigner.
TOOLBARS AND STATUS BARS