Udostępnij za pośrednictwem


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.

XAML editor

XAML Window

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.

link to video For a video version of this topic, see Video How to: Create a C# WPF Application.

To create a WPF application

  1. 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.

  2. Click WPF Application.

  3. Change the name of your application to Ink Pad.

  4. Click OK.

    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.

  5. 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.

  6. Change the size of the WPF window by setting the Height property to 550 and the Width property to 370 in the Properties window.

  7. Change the title of the WPF window to Ink Pad.

  8. Change the Background property of the WPF window to the color brown by clicking Brown in the drop-down box, and then pressing Enter.

    Note

    You can alternatively modify the XAML markup directly by adding a Background attribute and setting its value to Background="Brown".

  9. To open the Toolbox, click the View menu, and then click Toolbox.

  10. Right-click the Toolbox, and then click Choose Items.

    The Choose Toolbox Items dialog box opens.

  11. 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.

  12. Click OK to add the InkCanvas control to the Toolbox.

  13. From the General tab of the Toolbox, drag an InkCanvas control to the WPF window.

  14. Set the following properties of the InkCanvas control in the Properties window:

    Property

    Value

    Width

    Auto

    Height

    Auto

    HorizontalAlignment

    Stretch

    VerticalAlignment

    Stretch

    Margin

    9, 9, 9, 68

  15. 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.

  16. Drag two Button controls to the WPF Window under the InkCanvas. Position button1 to the left and button2 to the right.

  17. 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>
    
  18. 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

    WPF Ink Application

To create event handlers

  1. Double-click Clear, and then add the following code to the generated Click event handler:

        this.inkCanvas1.Strokes.Clear(); 
    
  2. Return to Designer view by right-clicking the Code Editor and then clicking Designer.

  3. Double-click Close, and then add the following code to the generated Click event handler:

        this.Close();
    
  4. Press F5 to run the project.

  5. When the application opens, draw a picture in the InkCanvas control. If you make a mistake, you can click Clear to start over.

  6. Click Close to the close the application.

See Also

Tasks

How to: Create a New WPF Application Project

How to: Create a C# Console Application

How to: Create a C# Windows Forms Application

Other Resources

Creating Your First Visual C# Application

Change History

Date

History

Reason

September 2008

Revised step-by-step instructions.

Customer feedback.