#189 – Adding an Event Handler to a Control Using Visual Studio

An event handler is code that executes in response to a user-initiated event–for example, code that executes when a user clicks a button.

In Visual Studio, you can add a new event handler in several ways.  You could edit the XAML directly and enter the name of a handler, or you can just double-click in the Properties window.

To add a new event handler from the properties window, start by left-clicking to select the control for which you want to add an event (e.g. a Button).

Next, find the desired event on the Events tab in the Properties window (e.g. Click).

Double-click in the empty area to the right of the event name, generating a name for the new handler (e.g. button1_Click)..

..and you’ll be taken to the code editor, where the body of the new event handler is located.

The name of the handler also now appears in XAML.

#187 – Adding Controls to a Window Using Visual Studio

In Visual Studio, you can add user interface controls to a window by dragging them onto a design surface.  Start by double-clicking on the window’s .xaml file in the Solution Explorer.

This will open the window in a design view editor.  The editor will have a design surface at the top and a XAML editor below that.

You can add new controls to the window by dragging them onto the design surface in the top pane or by editing the XAML directly.

To drag a control onto the design surface, find the list of WPF controls in the Toolbox window.  Once you find the control that you want, left-click and drag it onto the window’s design surface.  In the example below, we add a CheckBox to the window.

When you’re done dragging, the new control will show up both on the design surface and in the XAML.

#186 – When to Use Blend (vs. Visual Studio)

You could develop your entire application using only Visual Studio.  Actually, you could develop an application without Visual Studio, using your editor of choice to write the code and command line tools to build your application.

But ideally, you’ll want to use both Visual Studio and Blend.  There is a fair amount of overlap in what you can do in each tool, but the basic model is:

  • Use Visual Studio to write and debug code
  • Use Blend to create or edit layout of visual elements in your application

While you can use Visual Studio to do layout and you can use Blend to edit code, Visual Studio is better at working with code and Blend is better at working with layout.

 

#184 – Creating a WPF Application Using Visual Studio

You can create a basic WPF application using Visual Studio 2010.  Open Visual Studio and select File | New | Project.  Then select WPF Application as the template to use for creating the project.

This will generate a project with the project structure shown below.

The project contains:

  • An App class, deriving from System.Windows.Application
    • Has Main() method, defined in automatically generated App.g.i.cs file
    • Empty App.xaml file, defining main <Application> element
    • Empty App.xaml.cs file with partial class for App
  • A MainWindow class, deriving from System.Windows.Window
    • InitializeComponent() method, defined in auto-generated MainWindow.g.i.cs file, which reconstitutes object from XAML file
    • MainWindow.xaml file, containing <Window> object and a child <Grid>
    • Empty MainWindow.xaml.cs file with partial class for MainWindow

#183 – Project Types Available from Visual Studio 2010 and Expression Blend 4

You can create the following types of WPF-related projects from Visual Studio 2010:

  • WPF Application - Standard Windows WPF client application
  • Class Library - Library of reusable classes
  • WPF Browser Application - Page-based application that runs in browser
  • WPF User Control Library - Template for a new control, deriving from UserControl
  • WPF Custom Control Library - Template for new control, deriving from Control

You can create the following WPF-related projects from Expression Blend 4:

  • WPF Application - Standard Windows WPF client application
  • WPF Control Library - Template for a new control, deriving from UserControl
  • WPF Databound Application - Basic template for an MVVM application
  • WPF SketchFlow Application - Template for an application prototype, using the SketchFlow theming

#182 – Create a WPF Project from Either Visual Studio or Blend

You can create a new WPF project from either Visual Studio 2010 or Expression Blend 4.

Create a new project from Visual Studio 2010 using File | New | Project:

This opens the New Project dialog.

Create a new project from Blend 4 using File | New Project:

This opens Blend 4′s New Project dialog.

#181 – Creating a WPF Development Environment

To start developing WPF applications on Windows, you need to install Visual Studio 2010 on Windows.

The bare minimum setup includes:

A more typical professional development environment would include:

[Click here for a screen-by-screen example of installing Visual Studio C# 2010 Express).

#128 – Using Code Snippets to Implement a Dependency Property

Visual Studio 2010 includes a code snippet that makes it easier to implement a dependency property in your own class.

To start, right-click on an empty line in your class and pick Insert Snippet.

Double-click on the NetFX30 category.

Then select Define a DependencyProperty.

The code for the code snippet will be added.  (Alternatively, just enter “propdp” in the code editor and press TAB twice).

At this point, you can dbl-click on the property name or its type to enter new values.  When you change the value, it will be changed accordingly throughout the rest of the code snippet.

#90 – Stepping Into .NET Source Code in Visual Studio 2010

It’s sometimes helpful to see not only the metadata for .NET and WPF classes, but to actually step through the source code in the debugger to see how things are working.

To enable stepping through .NET source code in the debugger:

First, close any open solutions.  Then, under Tools | Options, select Debugging | General and check the option Enable .NET Framework source stepping.

Also check the Enable Source Server Support checkbox. Next, select Debugging | Symbols in the list at left.  Unselect Microsoft Symbol Servers, if it’s checked.  Click on the add button and enter http://referencesource.microsoft.com/symbols.  Then click on the Empty Symbol Cache button.

When you start debugging, you’ll see a message indicating that symbols are being downloaded.  This may take some time.

When you first try stepping into a .NET Framework method, you’ll see a EULA.

But then you’ll find yourself looking at .NET source code.  Voila!

Follow

Get every new post delivered to your Inbox.

Join 129 other followers