228Microsoft Visual Studio 2010: A Beginner’s Guide

In Figure 8-8, you can see the Toolbox with the Button control selected. The Designer shows a Button control that has been dragged and dropped. In practice, you’ll be adding this control into some type of layout control so that you can position it appropriately on the screen.

Below the Designer, the Button control appears in the XAML for this window. If you are uncomfortable looking at XAML, you can review Appendix B as a refresher. The attributes of the Button control in the XAML match the properties in the Properties window.

TIP

It’s important to learn how to quickly build UIs using the Visual Designer because it enhances productivity. However, it’s also important to be able to read the XAML associated with a window because as you move beyond the beginner content of this book, you’ll find scenarios where the Designer alone might not allow you to control every nuance of your visual presentation. A good way to move forward is to experiment on your own by adding each of the controls from the Toolbox to the Designer and then examine the generated XAML.

Setting Properties

The Properties window shows all of the ways that you can configure a control. For button controls, you’ll want to change the Content property to make the text on the button make sense. In this example, we’ll imagine that the purpose of the button is to allow a user to create a new order for a customer. Therefore, set the Content property to New Order.

Handling Events

In addition to properties, you can handle control events via the Events tab at the top of the Properties window. Figure 8-9 shows the contents of the Events tab.

Controls have literally dozens of events that allow you to manage their behavior in the application. Some events, like Click, are commonly used, while other events, such as Drag Over, only support unique scenarios like drag and drop that you might not ever care about. To handle an event, you can double-click any of the events in the Properties window and VS will wire up that event to a handler method with a default name.

Since the Click event is so common, I’ll show how it works. You can implement a handler for the Click event by double-clicking the Click event in the Properties window Events tab. When you double-click, VS opens a file named MainWindow.xaml.cs, assuming the window you’re working with is named MainWindow.xaml. MainWindow

.xaml.cs is called a code-behind file and is where you can add event handlers. VS also creates a skeleton method in MainWindow.xaml.cs that handles the Button Click event, shown in Listing 8-1.

Page 251
Image 251
Microsoft 9GD00001 manual Setting Properties, Handling Events