WPF: Expression Blend 2 and external resources (VS2008 version)

.NET, Blend, Technical stuff, WPF
See comments

Following a past entry about Blend and external resources, some of you told me that they tried it without success in Orcas. Here is an updated checklist for Orcas, which should solve the problem, I hope.

To make things clear: I use the latest version of Expression Blend (at the time of this writing, it’s the August CTP of Blend 2). With earlier versions of Blend, it probably won’t work as well, if at all.

Additionally, it is imperative to use the syntax I describe to “merge” the resource dictionary in the Window. To provide you with a good basis, I also saved a Visual Studio 2005 and a Visual Studio 2008 project on my webserver. Opening these projects in Blend should allow saving resources to external resources in the way I describe.

  • Create a new WPF application in Visual Studio 2008
    “File / New / Project / Windows / WPF Application”. Name the project “MyApplication”
  • In the Solution Explorer, right-click on the solution and select “Add / New project / WPF User Control Library”. Name this project “Skins”.
  • Select the UserControl1.xaml and delete it. This deletes both the XAML file and the C# file (code-behind).
  • Right-click on the Skins project and select “Add / New item / Resource Dictionary (WPF)”. Keep the name Dictionary1.xaml.
  • Right click on the project MyApplication and select “Add reference / (wait, wait) / Projects / Skins”. This creates a one-direction link between the Skins project and the MyApplication project.
  • Open the file Window1.xaml in the MyApplication project. Modify the XAML code to look this way:
<Window
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
x:Class=”MyApplication.Window1″
Title=”Window1″ Height=”300″ Width=”300″
>

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source=”/Skins;component/Dictionary1.xaml”/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

<Grid>
</Grid>
</Window>

This imports the resources defined in Dictionary1.xaml, in the external assembly, so that they will be parsed and made available to your controls.

  • Save everything, Build once to create the assemblies.

Now we will change to Expression Blend 2 and store a resource in the external Dictionary1.xaml

  • Open the solution in Expression Blend 2.
  • Open the file Window1.xaml.
  • Add a Button to the Window using the toolbar on the left
Toolbox, insert a button
  • Make sure that the Selection tool is selected in the toolbox. Right-click on the button and select “Edit Control Parts (Template) / Create Empty”.
  • This opens a dialog. Note how you can choose to store the new template in the resource dictionary “Dictionary1.xaml” even though it is located in an external assembly.
Create resource dialog
Previous entry | Next blog entry

Comments for WPF: Expression Blend 2 and external resources (VS2008 version)