New book: MCTS Self-Paced Training Kit (Exam 70-511): Windows Applications Development with Microsoft .NET Framework 4


New book: MCTS Self-Paced Training Kit (Exam 70-511): Windows Applications Development with Microsoft .NET Framework 4

  • Comments 4

627420cvr.inddWe’re happy to report that MCTS Self-Paced Training Kit (Exam 70-511): Windows Applications Development with Microsoft .NET Framework 4 (ISBN: 9780735627420; 672 pages) is available to purchase. This self-paced training kit is designed to help maximize your performance on 70-511, the required exam for the MCTS certification: Windows Application Development with Microsoft .NET Framework 4.

This 2-in-1 kit includes the official Microsoft study guide, plus practice tests on CD to help you assess your skills. It comes packed with the tools and features exam candidates want most—including in-depth, self-paced training based on final exam content; rigorous, objective-by-objective review; exam tips from expert, exam-certified authors; and customizable testing options. It also provides real-world scenarios, case study examples, and troubleshooting labs for the skills and expertise you can use on the job.

You can find the book’s table of contents in a previous post.

Here is an excerpt from the Training Kit:

C H A P T E R 1

 

Building a User Interface

 

The user interface is the visual representation of your application. Users of your application use the user interface to interact with the application through the manipulation of controls,
which are hosted in windows. Currently, you can use two Microsoft technologies in Visual Studio to build Microsoft Windows applications: Windows Forms and Windows Presentation Foundation (WPF).


Windows Forms historically has been the basis for most Microsoft Windows applications and can be configured to provide a variety of user interface (UI) options.
The developer can create forms of various sizes and shapes and customize them to the user’s needs. Forms are hosts for controls, which provide the main functionality of the UI.


WPF is the successor to Windows Forms for desktop application development. WPF applications differ from traditional Windows Forms applications in several ways, the most
notable of which is that the code for the user interface is separate from the code for application functionality. Although the code for the functionality of a project can be defined
using familiar languages such as Microsoft Visual Basic .NET or Microsoft Visual C#, the user interface of a WPF project is typically defined using a relatively new declarative syntax
called Extensible Application Markup Language (XAML).


Although this training kit does cover some elements of Windows Forms programming, the primary focus for this training kit and the exam for which it prepares you is WPF
technology.


This chapter introduces you to the fundamentals of creating a Windows application. Lesson 1 describes the kinds of WPF controls and how to use them. Lesson 2 explains using
resources, and Lesson 3 describes how to incorporate styles into your WPF application.


Exam objectives in this chapter:
■ Choose the most appropriate control class.
■ Implement screen layout by using nested control hierarchies.
■ Manage reusable resources.
■ Create and apply styles and theming.
■ Create data, event, and property triggers in WPF.


Lessons in this chapter:
■ Lesson 1: Using WPF Controls 3
■ Lesson 2: Using Resources 41
■ Lesson 3: Using Styles and Triggers 57

Before You Begin
To complete the lessons in this chapter, you must have:
■ A computer that meets or exceeds the minimum hardware requirements listed in the
“Introduction” section at the beginning of the book.
■ Microsoft Visual Studio 2010 Professional edition installed on your computer.
■ An understanding of Visual Basic or C# syntax and familiarity with Microsoft .NET
Framework 4.
■ An understanding of XAML.

 

image

Lesson 1: Using WPF Controls

 

In this lesson, you learn to use WPF controls for WPF application development and how to use individual controls, item controls, and layout controls, each of which is necessary for creating WPF applications.


image

WPF Controls Overview

 

There are three basic types of controls in WPF. First, there are individual controls, which correspond with many of the familiar controls from Windows Forms programming.
Controls such as Button, Label, and TextBox are familiar to developers and users alike. These controls generally have a single purpose in an application; for example, buttons
are clicked, text boxes receive and display text, and so on. A subset of these controls comprises content controls, which are designed to display a variety of kinds of content.
Content controls, discussed later in this lesson, typically contain a single nested element.


A second kind of WPF control is the item control, which is designed to contain groups of related items. Examples of these include ListBox controls, Menu controls, and TreeView controls. These controls typically enable the user to select an item from a list and perform an
action with that item. Item controls can contain multiple nested elements. These controls are discussed later in this lesson.


Finally, layout controls, which contain multiple nested controls of any type, provide built-in logic for the visual layout of those controls. Examples include Grid, StackPanel, and Canvas. These controls are also discussed later in this lesson.

Content Controls


Many of the controls you use to build your WPF application are content controls. Simply, a content control derives from the ContentControl class and can contain a single nested element.
This nested element can be of any type and can be set or retrieved in code through the Content property. The following XAML example demonstrates setting the content of a Button control to a string value (shown in bold):


<Button Height="23" Margin="36,0,84,15" Name="button2"
     VerticalAlignment="Bottom">This is the content string</Button>


You also can set the content in code, as shown in the following example:


Sample of Visual Basic Code
Button2.Content = "This is the content string"
Sample of C# Code
button2.Content = "This is the content string";


The type of the Content property is Object, so it can accept any object as content. How content is rendered, however, depends on the type of the object in the Content property. For items that do not derive from UIElement,
the ToString method is called, and the resulting string is rendered as the control content. Items that derive from UIElement, however, are displayed as contained within the content control. The following example code
demonstrates how to render a button that has an image as its content:


<Button Margin="20,20,29,74" Name="button1">
     <Image Source="C:\Pictures\HumpbackWhale.jpg"/>
</Button>


Assuming that the path to the image is valid, this code will render a button that displays a picture file of a humpback whale named HumpbackWhale.jpg.

Note that even though content controls can contain only a single nested element, there is no inherent limit on the number of nested elements that the content can contain. For example, it is possible for a content control
to host a layout control that itself contains several additional UI elements. The following code shows a simple example of Button with a nested StackPanel control that itself has nested elements:


<Button Margin="20,20,-12,20" Name="button1">
    <StackPanel>
            <Image Source="C:\Pictures\HumpbackWhale.jpg"></Image>
            <TextBlock>This is a Humpback Whale</TextBlock>
   </StackPanel>
</Button>


At run time, this will be rendered as an image of a humpback whale with text beneath it.

 

Label Control and Mnemonic Keys

 

The Label control is one of the simplest WPF controls. It is mostly just a container for content. Typical usage for a Label control is as follows:


<Label Name="label1">This is a Label</Label>


Labels contain built-in support for mnemonic keys, which move the focus to a designated control when the Alt key is pressed with the mnemonic key. For example, if R were the mnemonic key for a particular control, the focus would shift to that control when Alt+R is pressed.
Typical usage for mnemonic keys in labels occurs when the label designates a control that can receive the focus, such as TextBox. The mnemonic key is specified by preceding the desired key with the underscore (_) symbol and appears underlined at run time when the Alt key is pressed. For example, the following code appears as Press Alt+A at run time when you press the Alt key:


<Label>Press Alt+_A</Label>


Although this code designates the mnemonic key for the label, it has no effect unless you designate a target control as well. You can designate a target control by setting the Target property of the Label control. The following example demonstrates how to create a mnemonic key with a target control named TextBox1:


<Label Target="{Binding ElementName=TextBox1}" Height="27"
   HorizontalAlignment="Left" VerticalAlignment="Top" Width="51">_Name
</Label>
<TextBox Name="TextBox1" Margin="53,1,94,0" Height="26"
   VerticalAlignment="Top">
</TextBox>


The syntax exemplified by {Binding ElementName=TextBox1} will be discussed further in Chapter 6, “Working with Data Binding.”

 

Button Control

 

The Button control should be familiar to most developers. This control is designed to be clicked to enable the user to make a choice, to close a dialog box, or to perform another action. You can execute code by clicking the button to handle the Click event. (For information about handling events, see Chapter 2, “Working with Events and Commands.”)


The Button control exposes two important properties useful when building user interfaces: the IsDefault property and the IsCancel property.


The IsDefault property determines whether a particular button is considered the default button for the user interface. When IsDefault is set to True, the button’s Click event is raised when you press Enter. Similarly, the IsCancel property determines whether the button should
be considered a Cancel button. When IsCancel is set to True, the button’s Click event is raised when Esc is pressed.


ACCESS KEYS
Buttons provide support for access keys, which are similar to the mnemonic keys supported by labels. When a letter in a button’s content is preceded by an underscore symbol (_), that letter will appear underlined when the Alt key is pressed, and the button will be clicked when the user presses Alt and that key together. For example, assume you have a button defined as follows:


<Button>_Click Me!</Button>


The text in the button appears as “Click Me” when Alt is pressed, and the button is clicked when Alt+C is pressed. If more than one button defines the same access key, neither is activated when the access key combination is pressed, but focus alternates between the buttons that define that key.

CHECKBOX CONTROL
The Checkbox control actually inherits from the ButtonBase class and typically enables the user to select whether an option is on or off. You can determine whether a check box is selected by accessing the IsChecked property. The IsChecked property is a Boolean? (bool? in C#) data type similar to the Boolean type but allows an indeterminate state as well. A check box will be in the indeterminate state when a window first opens.
Because Checkbox inherits from ButtonBase, it raises a Click event whenever the user selects or clears the check box. The best way to react to the user selecting or clearing a check box is to handle the Click event.

RADIOBUTTON CONTROL
Like Checkbox, RadioButton inherits from the ButtonBase class. RadioButton controls are typically used in groups to enable the user to select one option from a group. Clicking a radio button causes the Click event to be raised to react to user choices.
A fundamental feature of RadioButton controls is that they can be grouped. In a group of RadioButton controls, selecting one automatically clears all the others. Thus, it is not possible for more than one radio button in a group to be selected at one time.
Usually, all RadioButton controls in a single container are automatically in the same group. If you want to have a single group of three RadioButton controls in a window, all you need to do is add them to your window; they are automatically grouped. You can have multiple groups in a single container by setting the GroupName property. The following example demonstrates two groups of two radio buttons each.


<RadioButton GroupName="Group1" Name="RadioButton1" Height="22"
  
VerticalAlignment="Top" Margin="15,10,0,0"
   HorizontalAlignment="Left" Width="76">Button 1</RadioButton>
<RadioButton GroupName="Group1" Name="RadioButton2"
   Margin="15,34,0,0" Height="22" VerticalAlignment="Top"
   HorizontalAlignment="Left" Width="76">Button 2</RadioButton>
<RadioButton GroupName="Group2" Name="RadioButton3"
   Margin="15,58,0,0" Height="21" HorizontalAlignment="Left"
   VerticalAlignment="Top" Width="76">Button 3</RadioButton>
<RadioButton GroupName="Group2" Name="RadioButton4"
   Margin="15,85,0,0" Height="22" HorizontalAlignment="Left"
   VerticalAlignment="Top" Width="76">Button 4</RadioButton>


You also can create groups of radio buttons by wrapping them in containers, such as in the code shown here:


<StackPanel Height="29" VerticalAlignment="Top">
   <RadioButton Name="RadioButton1">Button 1</RadioButton>
   <RadioButton Name="RadioButton2">Button 2</RadioButton>
</StackPanel>
<StackPanel Height="34" Margin="0,34,0,0" VerticalAlignment="Top">
   <RadioButton Name="RadioButton3">Button 3</RadioButton>
   <RadioButton Name="RadioButton4">Button 4</RadioButton>
</StackPanel>

image

Other Controls

 

There are other controls in the WPF suite that are not content controls. They do not have a Content property and typically are more limited in how they display or more specialized in terms of the content they display. For example, the TextBlock control displays text, and the Image control represents an image.


TextBlock Control
TextBlock is one of the simplest WPF elements. It just represents an area of text that appears in a window. The following example demonstrates a TextBlock control:


<TextBlock>Here is some text</TextBlock>


If you want to change the text in a TextBlock control in code, you must set the Name property of the TextBlock control so that you can refer to it in code, as shown here:


<TextBlock Name="TextBlock1">Here is some text</TextBlock>

Then you can change the text or any other property by referring to it in the code, as shown here:


Sample of Visual Basic Code
TextBlock1.Text = "Here is the changed text"
Sample of C# Code
TextBlock1.Text = "Here is the changed text";


By default, the font of the text in the TextBlock element will be the same as the font of the window. If you want different font settings for the TextBlock, you can set font-related properties, as shown here:


<TextBlock FontFamily="Batang" FontSize="12"
   FontStyle='Italic' FontWeight='Bold'
   FontStretch="Normal">Here is some text</TextBlock>


Image Control
The Image control represents an image. The chief property of the Image control is the Source property, which takes a System.Windows.Media.ImageSource class in code, but, when set in XAML, it can be set as the Uniform Resource Identifier (URI) from which the image is loaded. For example, look at the following code.


<Image Source="C:\Pictures\Humpbackwhale.jpg"/>


The URI can be either a local disk resource or a Web resource.
The Image.Stretch property determines how an image is displayed, whether it is shown at actual size and cropped (if necessary) to fit the image bounds, or whether it is shrunk or stretched to fit the bounds of the Image control. Table 1-1 describes the possible values for the Stretch property.

image

  • Is the an eBook version?

  • There will be an ebook version soon, we hope. At the moment, we have stopped selling ebook versions of our Training Kits because we do not have a way to offer the practice test and other companion content that is on the CD with the print version. When we have a solution in place, we'll announce it on this blog and our Twitter account.

  • Where can I submit corrections?  There are a small number of errors in the book, and a lot of errors in the practice questions.

    Cheers.

  • Hi, Johnny. Use the "View/Submit Errata" link at the book's catalog page: oreilly.com/.../9780735627420  Thank you in advance for your corrections!

Page 1 of 1 (4 items)
Leave a Comment
  • Please add 7 and 5 and type the answer here:
  • Post