They say a picture is worth a thousand words. I’m sure the above picture certainly got your attention. Let’s understand how you can do this in a few minutes in your projects TODAY.
This simple solution significantly raises the productivity bar when working with DataTemplates, DataGrids, ListBoxes or ListViews because the Cider Designer will simply re-render as you edit the XAML.
You no longer have to keep running the project and tweaking between runs, just follow these super simple instructions and the Cider Designer will be your new best friend. This solution works in WPF and Silverlight TODAY.
Background
I came into work earlier this week and mentioned to Mark Boulter a Microsoft Principal Program Manager Lead on the Cider Team that I was working on a complex ComboBox DataTemplate the previous night and I wished that I could have accomplished the task with design time data displayed in the Cider Designer. That this would have made the DataTemplate editing task much easier for me.
He just came out and said, “you can.” Most of you that know me, can picture Karl’s ears perking up and an above indoor voice replying with excitement, “what!”
There is no magic, no monkey business going on. The Visual Studio 2008 Cider Designer instantiates controls and renders them on the design surface. If that control has a DataContext with data in it and the control is data bound, the data displays. It’s that simple.
I’m not sure why this light did not come on in my brain before. Of course Cider instantiates controls. It has to so that it can render the control properly when properties like Background and Width are set on the control. This is why all controls must have a default empty constructor, so that they can be instantiated by the designer.
So with 5 minutes of brain storming and 15 minutes of trail and error coding, Mark, Ben and I came up with the solution that is being presenting here. Ben Wulfe is a Microsoft Senior Software Development Engineer on the Cider Team.
We sincerely hope you find this solution easy to use and that you get a greater design time experience by using it.
So as many of my super geek friends would say, “let’s feel the love and write some code!”
The Data
The Customer class is a from the WPF project and is your typical WPF business entity object. Customers represents the collection of Customer. These are similar to your standard classes already in your current WPF and Silverlight projects.
The DesignTimeCustomers class provides the Cider Designer the design time data. Rather than create a special design time data class with the same shape as Customer and Customers, I chose to derive from Customers. Now I’m guaranteed that any breaking changes to Customers or Customer will cause a compile time error and I can quickly resolve the issue.
I’ve also placed DesignTimeCustomers in its own Namespace to avoid any problems on large projects like a developer using a design time class instead of a real run-time class.
Now for the secret sauce. By loading the individual Customer objects into the DesignTimeCustomers collection in the constructor, I have a class that when instantiated will be a class with its collection fully populated and ready to be consumed, even at design time.
Namespace DesignTimeData Public Class DesignTimeCustomers Inherits Customers Public Sub New() Me.Add(New Customer(True, 55000, 1, "Tim", "Smith")) Me.Add(New Customer(True, 75000, 2, "John", "Johnson")) Me.Add(New Customer(True, 52000, 3, "Susan", "Washington")) End Sub End Class End Namespace
For each of the DataTemplates, DataGrids, ListBoxes, ListViews or ComboBoxes you need design time data viewing, you’ll create a class similar to the above. I was thinking about writing and Add-In that creates these classes but that’s for another day.
The Resource
This solution has two methods of implementation.
- Solution One: Design Time and Run-Time DataContext – you assign the DataContext to a resource that points to the above DesignTimeCustomers
- Solution Two: Different Design Time and Run-Time DataContext – using the DesignAndRunTimeDataContext class allows specifying both a design time and run-time DataContext. This provides the ability to consume the above DesignTimeCustomers class at design time and when the project is run, the DataContext can be set in code, displaying records from a data base for example. This class only works in WPF because Silverlight does not have a DataSourceProvider class which is what DesignAndRunTimeDataContext derives from.
I think implementation comes down to each developer’s workflow. If you want automatic design time and run-time DataContext switching, use the second solution above. Otherwise use the first solution.
Solution One
<UserControl x:Class="SilverlightTestingDesignTimeData.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DesignTimeData="clr-namespace:SilverlightTestingDesignTimeData.DesignTimeData" Width="325" Height="200"> <UserControl.Resources> <DesignTimeData:DesignTimeCustomers x:Key="designTimeCustomersDS" /> </UserControl.Resources>
The above code snippet is from the Silverlight demo project included in the project downloads. I’ve just created a resource that points to the above DesignTimeCustomers class. The resource will be instantiated by the Cider Designer at design time. Since the collection is populated in its constructor, we now have a populated collection that can be consumed at design time. How cool is that!
Solution Two
Solution two can be implement one or more ways also.
Below the DesignTimeDataContext and the RunTimeDataContext have both been assigned to the same resource. I did this to keep the example as simple as possible. If I had a resource that pointed to another collection at run-time I could have used it.
Don’t forget to add the namespace declaration for DesignTimeData.
<Window x:Class="DesignTimeDataTwo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DesignTimeData="clr-namespace:VisualStudio2008DesignTimeData.DesignTimeData" xmlns:local="clr-namespace:VisualStudio2008DesignTimeData" Title="Design Time Data Demo Two" Height="300" Width="300"> <Window.Resources> <DesignTimeData:DesignTimeCustomers x:Key="designTimeCustomersDS" /> <local:DesignAndRunTimeDataContext x:Key="customersDS" DesignTimeDataContext="{StaticResource designTimeCustomersDS}" RuntimeDataContext="{StaticResource designTimeCustomersDS}" /> </Window.Resources>
In the below code the DesignTimeDataContext is being set in XAML and the RunTimeDataContext is being set in the code behind file at run-time.
<Window x:Class="DesignTimeDataTwo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DesignTimeData="clr-namespace:VisualStudio2008DesignTimeData.DesignTimeData" xmlns:local="clr-namespace:VisualStudio2008DesignTimeData" Title="Design Time Data Demo Two" Height="300" Width="300"> <Window.Resources> <DesignTimeData:DesignTimeCustomers x:Key="designTimeCustomersDS" /> <local:DesignAndRunTimeDataContext x:Key="customersDS" DesignTimeDataContext="{StaticResource designTimeCustomersDS}" /> </Window.Resources>
At run-time when the Window loads the below code assigns run-time data to the RunTimeDataContext property and then the DesignAndRunTimeDataContext.Refresh method is called which will cause this custom DataSourceProvider to re-query its data. You can have a look at the DesignAndRunTimeDataContext class in the WPF project included in the downloads below. Its a very simple class that provides DataContext switching between design time and run-time.
Partial Public Class DesignTimeDataTwo Private Sub DesignTimeDataTwo_Loaded( _ ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Dim objDS As DesignAndRunTimeDataContext = _ CType(Me.FindResource("customersDS"), DesignAndRunTimeDataContext) 'could load any data here, I'm just using what we have for grins objDS.RuntimeDataContext = New DesignTimeData.DesignTimeCustomers objDS.Refresh() End Sub End Class
The DataContext
Wiring up the DataContext to the above resource is very simple. The ListBoxes ItemsSource property is also set and the ListBox performs a data binding operation at design time.
<Grid DataContext="{StaticResource customersDS}" > <ListBox x:Name="lbCustomer" ItemsSource="{Binding}">
Data Triggers
After I did the video, I got to thinking about Data Triggers, so I wrote this simple Data Trigger to see if the Cider Designer would render it or not. Oh yes! It works great!
<DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=Id}" Value="1"> <Setter TargetName="tbId" Property="Foreground" Value="Red" /> </DataTrigger> </DataTemplate.Triggers>
Look close at the below image, notice that the Id for the first row has a Foreground of Red. This should give you enough to go with and learn to use the Data Triggers in your Data Templates.
Actual Usage
Don’t forget to build your projects after editing the entity classes.
I have seen an error sometimes when working with this solution in Silverlight projects. If you get an error when loading the designer, just leave the designer displayed and rebuild the project; the Cider Designer will correctly render your UserControl content.
Blend Compatibility
Guess I really don’t need to say much. Here is Blend 2 with Blend SP1 applied editing a ListBox DataTemplate with design time data using this solution. You can read about the latest Blend releases on Scott Guthrie’s blog.
After I posted this solution my friend and Microsoft MVP Laurent Bugnion wrote me about a solution he came up with last year that focuses on Blend.
Here is the link to his article:
Also of interest, he has a sample WPF application demonstrating this technique:
Thanks Laurnet!
Let’s Go!
Mark, Ben and I hope this simple solution provides each Visual Studio 2008 WPF & Silverlight developer a boost in design time productivity using the Cider Designer. I don’t know about you, but I’m so juiced over this and now look forward to that next complex DataTemplate or DataGrid layout task!
Videos

The video links require Microsoft Silverlight 1.0. If you do not have it, you will be prompted to install it when you click one of the links. After the short installation is completed, close the browser window that you did the install in and re-click the video you want to watch. You can also download it here. Windows XP or Vista required.
Design Time Data Tutorial (only 5:46 long)
Downloads
After downloading the below package you must rename the extension from .DOC to .zip. This is a requirement of WordPress.com.
Download Source WPF Project 279KB
Download Silverlight Source 548KB
Have a great day and feel the love!
Just a grain of sand on the worlds beaches.




October 11, 2008 at 1:01 pm |
Bravo! This is great!!
October 11, 2008 at 1:05 pm |
Thanks Josh. We appreciate the encouragment and hope everyone finds this usage tip helpful.
Cheers,
Karl
October 11, 2008 at 2:52 pm |
Karl – this is fantastic stuff, and is being added to my workflow now. I owe you one man.
October 11, 2008 at 4:34 pm |
Gland you like the solution Pete!
October 12, 2008 at 5:02 am |
Hey Karl,
We talked about that already, but thought I would point to my blog article showing a similar technique (though back then I concentrated on Blend).
http://blog.galasoft.ch/archive/2007/09/14/WPF-Simulating-data-in-design-mode-in-Microsoft-Expression-Blend.aspx
Also of interest, I have a sample WPF application demonstrating this technique:
http://blog.galasoft.ch/archive/2008/03/27/techdays-2008-slides-and-code-of-my-presentation-available-online.aspx
Really happy to see your article, and how it works in Cider and Silverlight too. By joining our forces, we will fight the evil forces of ignorance my friend
Cheers,
Laurent
October 12, 2008 at 8:06 am |
Laurent,
Thanks for posting this comment on your work too! Anything we can do to increase developer/designer productivity is fantastic.
I love simple solutions to problems that bring a huge return.
Looking forward to seeing you in March at the MVP Summit.
Cheers,
Karl
October 12, 2008 at 5:23 pm |
[...] Viewing Design Time Data in Visual Studio 2008 Cider Designer in WPF and Silverlight Projects (Karl Shifflett) [...]
October 14, 2008 at 5:56 am |
[...] http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time-data-in-visual-studio-2008-cider-d... Category: Uncategorized You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Leave a Reply [...]
October 16, 2008 at 5:26 pm |
[...] up about paging data in Silverlight as it’s needed … and has this all written up… very cool! Viewing Design Time Data in Visual Studio 2008 Cider Designer in WPF and Silverlight Projects Karl Shifflett blogs about a way to get data displayed on the design time surface of VS2008… no [...]
October 20, 2008 at 4:03 am |
Interesting approach. So far I’ve solved design time data by using a M-V-VM pattern, and have my ViewModel take any dependency to data services as interfaces in the constructor (DI). Then I use a IoC container to inject either real services or mock services depending if the code is executing in Blend (design time) or in the browser (real implementations).
http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx shows that approach.
Cool stuff, and really interesting topic. We need to write code designers can consume to really leverage the designer-developer workflow.
- jonas
October 22, 2008 at 11:54 pm |
[...] Viewing Design Time Data in VS 2008 WPF and Silverlight Designers: Karl Shifflett has another nice article that talks about some techniques you can use to see sample data in the VS 2008 WPF and Silverlight designers when building applications. [...]
October 23, 2008 at 12:03 am |
[...] Viewing Design Time Data in VS 2008 WPF and Silverlight Designers: Karl Shifflett has another nice article that talks about some techniques you can use to see sample data in the VS 2008 WPF and Silverlight designers when building applications. [...]
October 24, 2008 at 12:31 pm |
[...] Viewing Design Time Data in Visual Studio 2008 Cider Designer in WPF and Silverlight Projects [...]
October 24, 2008 at 3:35 pm |
[...] Viewing Design Time Data in Visual Studio 2008 Cider Designer in WPF and Silverlight Projects [...]
October 25, 2008 at 10:42 pm |
[...] are shorter in length and more granular. The Form, DataGrid & ListView videos all use the Design Time Data Feature to enhance the video with live design time data during the layout [...]
November 3, 2008 at 2:29 pm |
[...] Blog Karl Shifflett : Karl has written an interesting article about displaying data in design time in WPF and Silverlight projects. [...]
November 5, 2008 at 11:03 am |
Ooh, this is good stuff. I love the power of databinding, but hated sacrificing designability. After thinking about your post for awhile, I though of another way, and I wondered what an expert like yourself would think of it.
What I did was come up with attached properties (which the Cider and Blend designers also instantiate) that can be used to set the DataContext or ItemsSource when the control is is design mode:
local:DesignMode.DataContext=”{x:Static local:Customers.DesignInstance}”
local:DesignMode.ItemsSource=”{x:Static local:Customers.DesignInstance}”
(I prefer to define a static DesignInstance property on my classes to expose the design mode data, but using static resource would also work.)
Here’s the implementation for the DataContext attached property:
public static class DesignMode { public static readonly DependencyProperty DataContextProperty = DependencyProperty.RegisterAttached("DataContext", typeof(object), typeof(DesignMode), new PropertyMetadata(new PropertyChangedCallback(OnDataContextChanged))); public static object GetDataContext(DependencyObject sender) { if (sender == null) throw new ArgumentNullException("sender"); return sender.GetValue(DataContextProperty); } public static void SetDataContext(DependencyObject sender, object dataContext) { if (sender == null) throw new ArgumentNullException("sender"); sender.SetValue(DataContextProperty, dataContext); } private static void OnDataContextChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { FrameworkElement element = (FrameworkElement)dependencyObject; object dataContext = e.NewValue; if (element != null) { if (DesignerProperties.GetIsInDesignMode(element)) { element.DataContext = dataContext; } } } // I also implemented a similar DependencyProperty for ItemsSource }November 5, 2008 at 7:52 pm |
Brent,
Nice solution. There are so many ways to make this happen. I wrote this because it was such good news for me. I use live design data everyday now and the design experiece looks naked if I don’t. Visual Studio 2008 is really a great product.
Again, very nice post.
Have a super day!
Karl
December 4, 2008 at 4:10 am |
[...] רוצים לראות את הנתונים בזמן עיצוב (Desgin Time). בפוסט הזה מציג קארל דרך להצגה של "נתוני זמן עיצוב" (Design Time [...]
December 16, 2008 at 1:41 pm |
Hi Karl,
Great post and a really, really useful technique. I’ve been playing with this today and not only do I understand it, it works too (after I’d removed some silly typos I made).
Do you have any thoughts on the following scenario? :
My test application is a window that has number of instances of the same user control.
I’ve successfully followed your advice above to allow me to design the user control with test data in place by creating a resource that I can bind to in the user control.
For the window I have a resource with a difference set of test data and ideally when I’m editing the userControl I’d like to see it’s test data (a single instance of the data) and when I’m editing the window I’d like to see the windows test data (a list so I’d get different views of the userControl as at the moment I’m getting each instance of it looking the same as the test data that is active is that in the userControl).
I’ve tried in an equivalent to the runtime/design time classes to work out whether I already have a dataContext but I’m getting nowhere fast.
I know this doesn’t make much sense but I appreciate any time, comments and experience you can pass on.
Many thanks
Chris
December 17, 2008 at 9:12 am |
I think I’ve cracked it (to some extent anyway, it’s bound to be really dodgy and reliant on the structure of my xaml no doubt but there’s something to go on here I’m sure).
I switched to Brent’s approach above but added a little bit:
if (DesignerProperties.GetIsInDesignMode(element))
{
if (parentIsNull)
{
element.DataContext = dataContext;
}
else
{
element.ClearValue(DataContextProperty);
}
}
Now, when my user control has this in the xaml:
(bound to a single instance)
and the main app window with a list of the user controls in it has
(bound to a list)
When I’m editing my user control I see the data for the single item (because element.parent is null) and when I’m editing the window I see the items from the list (parent is not null as the control is being used in a window)
Hope this helps someone. Probably as a starting point for something much better than I’ve come up with.
Chris
December 18, 2008 at 3:29 pm |
Chris,
Sounds like you got it sorted out, good job!
Best to you,
Karl
February 27, 2009 at 2:34 pm |
I have been struggling to find a good solution for sample data for use by designers that automatically is ignored at run time and in the end came up with a solution that I’m very happy with.
You can find the solution I came up with on my blog http://wpftricksandtips.blogspot.com/ .
Specifically it allows developers to create samples of their data in a way that Blend users can then bind to through the Blend UI.
March 18, 2009 at 10:31 am |
[...] a few tries, and looking at articles like this one and this one, it seemed that the road was going to be a bumpy one. Even though I got it working [...]
April 17, 2009 at 4:56 am |
[...] Viewing Design Time Data in VS 2008 WPF and Silverlight Designers: Karl Shifflett has another nice article that talks about some techniques you can use to see sample data in the VS 2008 WPF and Silverlight designers when building applications. [...]
May 25, 2009 at 11:56 am |
Hey Karl,
Love the sample you gave and so I played with it a bit to make it a little more accessible to those newer to WPF. I posted an updated version of the project on my blog http://theycallmemrjames.blogspot.com/ (though I’ve used the WPF-centric model for this version).
Thanks for making your stuff so available.
Cheers,
-James
May 25, 2009 at 1:14 pm |
James,
Glad you liked the code. I’ll take a look at your design too.
Cheers,
Karl
May 25, 2009 at 2:01 pm |
Thanks, Karl….as I said in my post, it’s really just your sample with a little re-organizing (were one to suppose how a larger project might work) and a basic implementation of some of the things you were suggesting. I actually wrote the post because of the design-time support I wish existed in the VS.Net designer, but had smudgelled that code together and wanted to share and give you cred for the heavy lifting.
Cheers,
-James
June 14, 2009 at 12:17 pm |
[...] Viewing Design Time Data in VS 2008 WPF and Silverlight Designers: Karl Shifflett has another nice article that talks about some techniques you can use to see sample data in the VS 2008 WPF and Silverlight designers when building applications. [...]