WPF: Data Binding in Expression Blend

.NET, Blend, Technical stuff, Work, WPF
See comments

Intro

Continuing my quest to make my applications as “blendable” as possible, and after noticing that Expression Blend doesn’t execute a Window’s or a User Control’s constructor when the element is displayed, I thought I’d try to place my test data in the XAML itself, using an XmlDataProvider. Unfortunately, it still doesn’t work as well as I had hoped.

Purpose

In order to allow designers to work in Expression Blend as much as possible, it is often necessary to create a test application. For example, the real data might not be available at design time. Or, if the data is available, it might be provided by an underlying service which Blend is unable to connect to (we saw in my previous post that Blend loads and executes the UI code in its AppDomain, but that the root element’s constructor is not executed).
Now I am wondering where is the best place for the test data. Placing it in the test application’s Window means that Blend cannot display it. Using a User Control to place the test data, and then placing the User Control in the Window allows Blend to display the data (again, see my previous post), however it doesn’t allow selecting the elements in the User Control in order to style them. It would be nice if we could, in one single Blend window, see all the databound elements and select them to modify them.

The last idea

As last attempt before (probably) giving up, I thought that if the test data was placed in the XAML code inline, using a XmlDataProvider, maybe it would work better and the databound elements would be represented in Blend. Unfortunately, it’s not the case.Here is what I tried:
Window containing a User Control:
<Window
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:src="clr-namespace:WindowsApplication4"
     Title="WindowsApplication4"
     Height="300" Width="300">
      <src:UserControl1/>
</Window>
User control, including inline XML data:
<UserControl
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="WindowsApplication4.UserControl1">
  <UserControl.Resources>
    <XmlDataProvider x:Key="data"
                     XPath="/Labels">
      <x:XData>
        <Labels xmlns="">
          <Label name="label1"/>
          <Label name="label2"/>
          <Label name="label3"/>
        </Labels>
      </x:XData>
    </XmlDataProvider>
    <DataTemplate x:Key="dataTemplate">
      <Border BorderBrush="Silver"
              BorderThickness="2"
              Background="Black"
              CornerRadius="8,8,8,8">
        <Label Content="{Binding XPath=@name}"
               FontSize="24"
               FontWeight="Bold"
               Foreground="Silver"/>
      </Border>
    </DataTemplate>
  </UserControl.Resources>
  <Grid DataContext="{StaticResource data}">
    <ListBox ItemsSource="{Binding XPath=Label}"
             ItemTemplate="{StaticResource dataTemplate}"
             Width="300"
             Height="300"/>
  </Grid>
</UserControl>
Opening this code in Blend creates the following result:
User control: No data is displayed
User control: No data is displayed
Window: The data contained in the User Control is displayed correctly
Window: The data contained in the User Control is displayed correctly
 

Conclusion

It’s time for me to give up on that quest. The designers will have to work on the templates (ControlTemplate, DataTemplate…) in Blend, will be able to see the results in Blend to some extent, but will need to run the application in order to see the whole extent of the functionalities. It’s not that big a deal, because the application can be compiled and run (in debug mode) from Expression Blend directly. This is actually a great feature, because most designers won’t install Visual Studio 2005. I will now work on recommendations for the WPF integrator for the workflow, maybe in the form of a checklist, and for the designers. In some cases, it is probably possible to work on the real, original application directly in Blend. In other more complex cases, a lean test application using the real, original resources will have to be built, and the WPF integrator will be in charge of this task.
Previous entry | Next blog entry

Comments for WPF: Data Binding in Expression Blend