#290 – Using Intellisense in Visual Studio

Intellisense in Visual Studio is a feature that provides a list of suggested code elements that you might want to enter next, based on what you’ve already typed.

As you start typing, Intellisense will suggest  both language elements and types that match what you’ve already entered.

In the example below, I’ve entered the letter ‘w’ and Intellisense suggests some possible elements that begin with ‘w’ that I might want to enter.

If I’m actually entering a while statement, I can use the down arrow key to highlight while in the list and then press TAB.  Intellisense fills in the rest of the while keyword for me.

If I press TAB a second time, Visual Studio will actually generate a code snippet that is a template for the entire while statement.

Also notice that when Intellisense suggests a class name, it provides general information about the class.

#288 – Editing WPF Code-Behind in Visual Studio

WPF applications are made up of markup (XAML) and code-behind (managed code).  The markup defines the layout and appearance of the application, whereas the code-behind defines the behavior.

You use the code editor in Visual Studio to edit code-behind.

You can open the code editor in several different ways.

Double-click the code file from the Solution Explorer.  For C#, this is a .cs file.

Right-click a .xaml file and select View Code.

Right-click anywhere in the design view editor and select View Code.  You can do this either on the design surface or in the XAML editor.

You can also press the F7 key while you have a .xaml file open in the design view editor, or have a .xaml file selected in the Solution Explorer, to open the corresponding code-behind file in the code editor.

#286 – Adding Controls to a Window in Visual Studio by Editing XAML

You can add a WPF control to a window by using the design view editor in Visual Studio.  You add the control by dragging and dropping it onto a design surface.

You can also add a control by editing the XAML directly while in the design view editor in Visual Studio.

To open the design view editor for a particular window, double-click the associated .xaml file in the Solution Explorer.

The editor will show up as a split screen, with the design surface on the top and the XAML that defines the window contents below.

You can add a control by manually entering XAML in the lower window.  Here’s the XAML for a simple button.

As you enter XAML in the lower half of the editor, the design surface in the top half of the window will update to reflect the changes to the XAML.

#193 – You Can Have a Project Open in Blend and Visual Studio at the Same Time

Visual Studio is better for some things than Blend (e.g. writing code) and Blend is better than Visual Studio at other things (e.g. changing layout).

This means that you may often switch back and forth between the tools.  However, instead of closing and reopening the project every time, you can have the project open in both tools at the same time.

This is typically not a problem, since you’re likely not changing the same file in both tools.

If you do happen to have the same file open in both tools and you make changes and then save your changes in one tool, the other tool will warn you that changes were detected.

Visual Studio detecting changes to a file:

Blend detecting changes to a file:

The tools will warn of changes only if you happen to have the same file open in both tools.

#191 – Setting a Control’s Properties Using Visual Studio

You can set a control’s properties in either Blend or Visual Studio.  To set properties for a control in Visual Studio, do the following:

Open the window containing the control.  Left-click to select the control.

Notice that all of the button’s properties are now listed on the Properties tab of the Properties window, at the right side of the main Visual Studio window.

You can change a property’s value by entering the value directly into the right column in this window.  When you click in this area, the appropriate editor will pop up, depending on the property’s type.  For example, clicking on the dropdown for the Background property causes a color selector to pop up.

Once you change a property value, you’ll see the new value reflected in the XAML for the control that you changed.  Notice that the value is converted to a textual representation.


#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