which will apply the resources application-wide and override the default styling of the controls in
your project with the corresponding ones defined in the theme file.
fiGure 18-25
372 .
chaPter 18 WindoWS preSenTATion FoundATion (WpF)
One last change to make is to set the background style for your
window(s) to use the style from the theme file (because this isn’t
automatically assigned). In your Window element add the following
attribute:
Background="{StaticResource WindowBackgroundBrush}"
Now run your project and you will find the controls in your form
look completely different, as shown in Figure 18-26.
To change the theme to a different one you can simply replace the Theme.xaml file with another one
from the WPF.Themes project and recompile your project.
fiGure 18-26
If you plan to extensively modify the styles and control templates for your
application you may find it much easier to do so in Expression Blend — a tool
specifically designed for graphics designers who are working with XAML.
Expression Blend is much better suited to designing graphics and animations
in XAML, and provides a much better designer for doing so than Visual
Studio (which is focused more toward developers). Expression Blend can open
up Visual Studio solutions and can also view/edit code and compile projects,
although it is really best suited to design-related tasks. This integration of Visual
Studio and Expression Blend helps to support the designer/developer workflow.
Both these tools can have the same solution/project open at the same time (even
on the same machine), enabling you to quickly switch between them when
necessary. If a file is open in one when you save a change to a file in the other a
notification dialog appears asking if you want to reload the file. To easily open
a solution in Expression Blend from Visual Studio, right-click a XAML file and
select the Open in Expression Blend option.
windows forMs interoPerability
Up until now you have seen how you can build a WPF application, however the likelihood is that
you already have a significant code base in Windows Forms and are unlikely to immediately migrate
it all to WPF. You may have a significant investment in that code base and not want to rewrite it all
for technology’s sake. To ease this migration path, Microsoft has enabled WPF and Windows Forms
to work together within the same application. Bi-directional interoperability is supported by both
WPF and Windows Forms applications, with WPF controls able to be hosted in a Windows Forms
application, and Windows Forms controls able to be hosted in a WPF application. This section
looks at how to implement each of these scenarios.
hosting a wPf control in windows forms
To begin with, create a new project in your solution to create the WPF control in. This control (for
the purpose of demonstration) will be a simple username and password entry control. From the
Windows forms interoperability .
373
Add New Project dialog (see Figure 18-27), select the WPF User Control Library project template.
This will already include the XAML and code-behind files necessary for a WPF user control. If you
examine the XAML of the control you will see that it is essentially the same as the original XAML
for the window you started with at the beginning of the chapter except that the root XAML element
is UserControl instead of Window.
fiGure 18-27
Rename the control to UserLoginControl, and add a grid,
two text blocks, and two textboxes to it as demonstrated in
Figure 18-28.
fiGure 18-28
In the code-behind add some simple properties to expose the
contents of the textboxes publicly (getters and setters):
Vb
Public Property UserName As String
Get
Return txtUserName.Text
End Get
Set(ByVal value As String)
txtUserName.Text = value
End Set
End Property
Public Property Password As String
Get
Return txtPassword.Text
End Get
374 .
chaPter 18 WindoWS preSenTATion FoundATion (WpF)
Set(ByVal value As String)
txtPassword.Text = value
End Set
End Property
c#
public string Username
{
get { return txtUserName.Text; }
set { txtUserName.Text = value; }
}
public string Password
{
get { return txtPassword.Text; }
set { txtPassword.Text = value; }
}
Now that you have your WPF control, build the project and create a new Windows Forms project
to host it in. Create the project and add a reference to your WPF project that contains the control
(using the Add Reference menu item when right-clicking the References in the project).
Open up the form that will host the WPF control in the designer. Because the WPF control library
you built is in the same solution, your UserLoginControl control will appear in the Toolbox and can
simply be dragged and dropped onto the form to be used. This automatically adds an ElementHost
control (which can host WPF controls) and references the control as its content.
However, if you need to do this manually the process is as follows. In the Toolbox there is a WPF
Interoperability tab, under which there is a single item called the ElementHost. Drag and drop this onto
the form, as shown in Figure 18-29, and you will
see that there is a smart tag that prompts you to
select the WPF control that you want to host.
Note that if the control doesn’t appear in the
drop-down you may need to build your solution.
The control will be loaded into the ElementHost
control and automatically given a name to refer
to it in code (which can be changed via the
HostedContentName property).
hosting a windows forms control in wPf
Now take a look at the opposite scenario — hosting a Windows Forms control in a WPF
application. Create a new project using the Class Library project template called Chapter 18
WinFormsControlLibrary. Delete the Class1 class, and add a new User Control item to the project
and call it UserLoginControl.
Open this item in the designer and add two text blocks and two
textboxes to it as demonstrated in Figure 18-30.
fiGure 18-30
In the code-behind add some simple properties to expose the contents of
the textboxes publicly (getters and setters):
fiGure 18-29
Windows forms interoperability .
375
Vb
Public Property UserName As String
Get
Return txtUserName.Text
End Get
Set(ByVal value As String)
txtUserName.Text = value
End Set
End Property
Public Property Password As String
Get
Return txtPassword.Text
End Get
Set(ByVal value As String)
txtPassword.Text = value
End Set
End Property
c#
public string Username
{
get { return txtUserName.Text; }
set { txtUserName.Text = value; }
}
public string Password
{
get { return txtPassword.Text; }
set { txtPassword.Text = value; }
}
Now that you have your Windows Forms control, build the project and create a new WPF project to
host it in. Create the project and add a reference to your Windows Forms project that contains the
control (using the Add Reference menu item when right-clicking the References in the project).
Open up the form that will host the Windows Forms control in the designer. Select the
WindowsFormsHost control from the Toolbox and drag and drop it onto your form. Unfortunately
at this point the designer can’t help you and you need to change to the XAML editor. You need to
add a namespace prefix definition on the root element:
xmlsn:wfapp="clr-namespace:Chapter18WinFormsControlLibrary;
assembly=Chapter18WinFormsControlLibrary"
And you can then modify the WindowsFormsHost element to host
your control, which when run will render the control as shown in
Figure 18-31.
<WindowsFormsHost x:Name="windowsFormsHost">
<wfapp:UserLoginControl x:Name="userLoginDetails" />
</WindowsFormsHost>
fiGure 18-31
376 .
chaPter 18 WindoWS preSenTATion FoundATion (WpF)
debuGGinG with the wPf Visualizer
Identifying problems in your XAML/visual tree at runtime can be difficult, but fortunately a
new feature called the WPF Visualizer has been added to VS2010 to help you debug your WPF
application’s visual tree. For example, an element may not be visible when it should be, may not
appear where it should, or may not be styled correctly. The WPF Visualizer can help you track down
these sorts of problems by enabling you to view the visual tree, view the values of the properties for
a selected element, and view where properties are getting their styling from.
In order to open the WPF Visualizer, you must first be in break mode. Using the Autos, Local, or
Watch tool window, find a variable that contains a reference to an element in the XAML document
to debug. You can then click the little magnifying glass icon next to a WPF user interface element
listed in the tool window to open the visualizer (as shown in Figure 18-32). Alternatively you can
place your mouse cursor over a variable that references a WPF user interface element (to display the
DataTip popup) and click the magnifying glass icon there.
fiGure 18-32
The WPF Visualizer is shown in Figure 18-33. On the left side of the window you can see the visual
tree for the current XAML document and the rendering of the selected element in this tree below it.
On the right side is a list of all the properties of the selected element in the tree, their current values,
and other information associated with each property.
fiGure 18-33
summary .
377
Because a visual tree can contain thousands of items, finding the one you are after by traversing the
tree can be difficult. If you know the name or type of the element you are looking for, you can enter
this into the search text box above the tree and navigate through the matching entries using the
Next and Prev buttons. You can also filter the property list by entering a part of the property name,
value, style, or type that you are searching for.
Unfortunately there’s no means to edit a property value or modify the property tree, but inspecting
the elements in the visual tree and their property values (and the source of the values) should help
you track down problems in your XAML much more easily than in previous versions of Visual
Studio.
suMMary
In this chapter you have seen how you can work with Visual Studio 2010 to build applications
with WPF. You’ve learned some of the most important concepts of XAML, how to use the unique
features of the WPF designer, looked at styling an application, and used the interoperability
capabilities between WPF and Windows Forms.
19
office Business applications
what’s in this chaPter?
.
Exploring the different ways to extend Microsoft Office
.
Creating a Microsoft Word document customization
.
Creating a Microsoft Outlook add-in
.
Launching and debugging an Office application
.
Packaging and deploying an Office application
Microsoft Office applications have always been extensible via add-ins and various automation
techniques. Even Visual Basic for Applications (VBA), which was widely known for various
limitations in accessing system files, had the capability to write applications that used an
instance of an Office application to achieve certain tasks, such as Word’s spell-checking
feature.
When Visual Studio .NET was released in 2002, Microsoft soon followed with the first release
of Visual Studio Tools for Office (known by the abbreviation VSTO, pronounced visto).
This initial version of VSTO didn’t really produce anything new except for an easier way of
creating application projects that would use Microsoft Word or Microsoft Excel. However,
subsequent versions of VSTO quickly evolved and became more powerful, allowing you to
build more functional applications that ran on the Office platform.
The latest version of VSTO was shipped as part of Visual Studio 2010. It provides several
enhancements over the previous version, including support for Office 2010, expanded support
for the Ribbon user interface, and improved packaging and deployment functionality.
This chapter begins with a look at the types of applications you can build with VSTO.
It then guides you through the process of creating a document-level customization to a
Word document, including a custom Actions Pane. Following this, the chapter provides a
380 .
chaPter 19 oFFice buSineSS ApplicATionS
walkthrough, showing how to create an Outlook add-in complete with an Outlook Form region.
Finally, the chapter provides some important information regarding the debugging and deployment
of Office applications.
choosinG an office ProJect tyPe
As you would expect, the types of applications you can create using VSTO under Visual Studio has
been updated since the previous version. You now have the ability to create applications that target
the new Microsoft Office 2010 applications, as well as Microsoft Office 2007 applications.
As with the previous version, add-in applications can be created for almost every product in the
Office suite including Excel, InfoPath, Outlook, PowerPoint, Project, Visio, and Word. In the case of
Excel and Word, these solutions can either be attached to a single document or be loaded every time
that application is launched.
You can create a new Office application by selecting File . New . Project. Select your preferred
language (Visual Basic or Visual C#), and then select the Office project category, as shown in
Figure 19-1.
fiGure 19-1
Two types of project templates are available for Office applications: document-level customizations
and application-level add-ins.
Choosing an office Project Type .
381
document-level customizations
A document-level customization is a solution that is based on a single document. To load the
customization, an end user must open a specific document. Events in the document, such as loading
the document or clicking buttons and menu items, can invoke event handler methods in the attached