How to: Create a C# WPF Application
The purpose of this topic is to show you how to create a simple Windows Presentation Foundation (WPF) application and become familiar with the Visual C# Express Edition integrated development environment (IDE). Like Windows Forms applications, WPF applications can be designed by dragging controls from the Toolbox to a design surface. In addition to having a designer, Properties window, and Toolbox, the IDE in WPF projects has a window that contains XAML. XAML is an acronym for Extensible Application Markup Language, which is used to create a user interface. The following illustration shows the location of the XAML editor.
This example shows you how to create your own Ink application, which enables you to draw pictures.
In this section, you'll learn how to:
Create a WPF application.
Toggle between Code view and Designer view.
Change the WPF window's properties.
Use the XAML editor.
Add an System.Windows.Controls.InkCanvas control.
Add a System.Windows.Controls.Button control.
Create event handlers for controls.
For a video version of this topic, see Video How to: Create a C# WPF Application.
To create a WPF application
On the File menu, click New Project.
The New Project dialog box appears. This dialog box lists the different default application types that Visual C# Express Edition can create.
Click WPF Application.
Change the name of your application to Ink Pad.
Visual C# Express Edition creates a new folder for your project that is named after the project title, and then displays your new WPF window, titled Window1, in Designer view. You can change to Code view at any time by right-clicking the designer surface or code window and selecting View Code. By default, the XAML editor is displayed under the designer, but you can see the XAML markup in full-screen mode by right-clicking the designer surface and clicking View XAML.
The WPF window you see in Designer view is a visual representation of the window that will open when your application is started. In Designer view, you can drag various controls from the Toolbox onto the WPF window. After you have dropped a control onto the WPF window, Visual C# automatically creates code that will cause the control to be positioned appropriately when the program is run.
If you cannot see the Properties window, on the View menu, click PropertiesWindow. This window lists the properties of the currently selected WPF window or control, and it's here that you can change the existing values.
Change the size of the WPF window by setting the Height property to 550 and the Width property to 370 in the Properties window.
Change the title of the WPF window to Ink Pad.
Change the Background property of the WPF window to the color brown by clicking Brown in the drop-down box, and then pressing Enter.
You can alternatively modify the XAML markup directly by adding a Background attribute and setting its value to Background="Brown".
To open the Toolbox, click the View menu, and then click Toolbox.
Right-click the Toolbox, and then click Choose Items.
The Choose Toolbox Items dialog box opens.
In the WPF Components tab of the Choose Toolbox Items dialog box, scroll down to InkCanvas, and select it so that a check appears in the check box.
Click OK to add the InkCanvas control to the Toolbox.
From the General tab of the Toolbox, drag an InkCanvas control to the WPF window.
Set the following properties of the InkCanvas control in the Properties window:
9, 9, 9, 68
Change the color of the InkCanvas control to yellow by setting its Background property to LightYellow.
The background color of the InkCanvas control will appear light yellow at run-time.
Drag two Button controls to the WPF Window under the InkCanvas. Position button1 to the left and button2 to the right.
Select button1 and change the XAML markup in XAML view as shown in the following markup. This markup sets the text to Clear.
<Button Height="23" HorizontalAlignment="Left" Margin="85,0,0,24" Name="button1" VerticalAlignment="Bottom" Width="75">Clear</Button>
Select button2 and change the XAML markup as shown in the following markup. This markup sets the text to Close.
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,72,24" Name="button2" VerticalAlignment="Bottom" Width="75" Click="ButtonCloseClicked">Close</Button>
The WPF application should resemble the Ink Pad application in the following illustration.
WPF Ink Application
To create event handlers
Double-click Clear, and then add the following code to the generated Click event handler:
Return to Designer view by right-clicking the Code Editor and then clicking Designer.
Double-click Close, and then add the following code to the generated Click event handler:
Press F5 to run the project.
When the application opens, draw a picture in the InkCanvas control. If you make a mistake, you can click Clear to start over.
Click Close to the close the application.
Revised step-by-step instructions.