Using Common WPF Controls
In this lesson, you will learn about some of the common Windows Presentation Foundation (WPF) controls that are available in the Toolbox when you create a WPF Application project.
WPF enables you to create visually enhanced user interfaces for your applications. Even the typical controls you're used to seeing in a standard Windows Forms application are enhanced in WPF applications.
The easiest way to add WPF controls to your application is to drag them from the Toolbox to the design surface. In their default state, the controls look much like the controls you'll find in a Windows Forms application. Controls in WPF support styles and templates that enable you to create visually compelling applications. To modify the appearance of a control, you can change its properties in the Properties window. You can also modify the appearance of a control by adding styles and attributes to the control in the XAML editor. Writing XAML on your own can be difficult, so you might want to consider using an application that can generate XAML for you, such as Expression Blend. For more information, see Collaboration with Expression Blend.
To add a control to the WPF window
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click WPF Application.
In the Name box, type WPFControls, and then click OK.
A new Windows Presentation Foundation project is created.
From the Toolbox, drag a TextBox control to the upper-right side of the WPF window.
Select the TextBox control.
Set the following properties for the text box in the Properties Window.
Add a Button control to the WPF window, next to the text box.
Change the text between the opening and closing Button tags in the XAML editor from Button to Add, as shown in the following example.
<Button Height="23" HorizontalAlignment="Right" Margin="0,59,35,0" Name="Button1" VerticalAlignment="Top" Width="75">Add</Button>
The text on the button changes after you type the new value.
Add a ListBox control to the WPF window, underneath the text box.
Double-click the button to enter the default event handler, and add the following code:
If TextBox1.Text IsNot "" Then ListBox1.Items.Add(TextBox1.Text) TextBox1.Text = "" End If
Press F5 to run your program. A window that contains the text box, list box, and button that you just added appears.
Type some text in the text box and then click the Add button. Verify that the text is added to the list box.
Add additional text to the list box.
The following table shows the most common WPF controls, which you can find on the Common Controls tab of the Toolbox.
Displays a border around content.
Enables a user to perform an action by clicking a button. The Buttonbase.Click event occurs when a Button is clicked.
Enables a user to select and clear a check box to indicate a Yes/No or True/False value.
Enables a user to select an item from a drop-down list. The list is displayed when the user clicks a drop-down arrow.
Defines a flexible grid area that consists of columns and rows.
Displays an image.
Displays text on a form. Provides support for access keys.
Enables a user to select an item from a list.
Enables a user to choose from among mutually exclusive items. The selection of one radio button is mutually exclusive to any other radio button in the same container.
Enables you to stack child controls vertically or horizontally.
Enables visual content to be arranged in a tabular form.
Displays unformatted text and enables users to enter text.
Additional controls available in the Toolbox include the following:
Container controls, such as Canvas, DockPanel, and Frame.
Menus and toolbars, such as Menu, ToolBar, and StatusBar.
Document controls, such as DocumentViewer and FlowDocumentPageViewer.
In this lesson, you learned about the common WPF controls that are available in the Toolbox. You also learned that you can customize these controls to make them visually compelling.
In the next lesson, you will learn how to create event handlers for a WPF control.
Next Lesson: Creating Event Handlers for WPF Controls.