<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicious.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Christopher Bennage : Silverlight, Blend</title><link>http://devlicious.com/blogs/christopher_bennage/archive/tags/Silverlight/Blend/default.aspx</link><description>Tags: Silverlight, Blend</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>d:DesignInstance in Blend 4</title><link>http://devlicious.com/blogs/christopher_bennage/archive/2010/03/29/d-designinstance-in-blend-4.aspx</link><pubDate>Mon, 29 Mar 2010 20:12:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:56325</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;Let’s walk through an example of using d:DesignInstance for improving the design-time experience in Blend. For background, see my &lt;a href="http://devlicio.us/blogs/christopher_bennage/archive/2010/03/26/exploring-design-time-data-in-blend-4.aspx" target="_blank"&gt;last post&lt;/a&gt; on the topic.&lt;/p&gt;  &lt;p&gt;I’m going to be using the ContactManager sample from the &lt;a title="free WPF and Silverlight from the stone" href="http://www.codeplex.com/caliburn"&gt;Caliburn&lt;/a&gt; trunk. You can access the source of it &lt;a href="http://caliburn.codeplex.com/sourcecontrol/network/Show?projectName=caliburn&amp;amp;changeSetId=46266" target="_blank"&gt;here&lt;/a&gt; under: \trunk\samples\LOB Samples\Silverlight\ContactManager\&lt;/p&gt;  &lt;p&gt;We’re only going to concern ourselves with one view/viewmodel pair:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ContactListView &lt;/li&gt;    &lt;li&gt;ContactListPresenter &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;(This sample code still uses Caliburn’s old naming scheme that was influenced by the MVP pattern. “Presenter” here means “View Model” for all practical purposes.)&lt;/p&gt;  &lt;p&gt;ContactListView displays a list of contacts in an ItemsControl on left side of the view. As you can see, there isn’t much visible in Blend.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="The view opened in Blend 4." border="0" alt="The view opened in Blend 4." src="http://devlicious.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/christopher_5F00_bennage/image_5F00_7C4AB7F3.png" width="476" height="400" /&gt; On the corresponding viewmodel, we have a property named Contacts that we’ll bind to the ItemsControl.&lt;/p&gt;  &lt;p&gt;(Note that Caliburn is binding to this property using naming conventions, but we’ll need to add an explicit binding in order to play nice with Blend.)&lt;/p&gt;  &lt;p&gt;Here’s what my ItemsControl looks like:&lt;/p&gt;  &lt;pre class="xml:nogutter:nocontrols" name="code"&gt;&amp;lt;ItemsControl x:Name=&amp;quot;Contacts&amp;quot; 
              ItemsSource=&amp;quot;{Binding Contacts}&amp;quot;&amp;gt;
&amp;lt;!-- omitted for brevity --&amp;gt;
&amp;lt;/ItemsControl&amp;gt;&lt;/pre&gt;

&lt;p&gt;Let’s select the ItemsControl in Blend and examine the data binding for the ItemsSource.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="selecting the data binding editor for the ItemsSource" border="0" alt="selecting the data binding editor for the ItemsSource" src="http://devlicious.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/christopher_5F00_bennage/image_5F00_74531591.png" width="291" height="556" /&gt; &lt;/p&gt;

&lt;p&gt;It’s a sad, empty little dialogue that greets us.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="The depressingly empty Data Context tab." border="0" alt="The depressingly empty Data Context tab." src="http://devlicious.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/christopher_5F00_bennage/image_5F00_13298970.png" width="616" height="578" /&gt; &lt;/p&gt;

&lt;h3&gt;Getting the Shape of Our Data Context&lt;/h3&gt;

&lt;p&gt;Let’s open up the XAML for ContactListView. In the root element, we’ll set the design-time data context using the d:DesignInstance markup extension. It looks like this:&lt;/p&gt;

&lt;pre class="xml:nogutter:nocontrols" name="code"&gt;&amp;lt;UserControl xmlns=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
             xmlns:x=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
             xmlns:Controls=&amp;quot;clr-namespace:Caliburn.Silverlight.ApplicationFramework.Controls;assembly=Caliburn.Silverlight.ApplicationFramework&amp;quot;
             xmlns:rm=&amp;quot;clr-namespace:Caliburn.PresentationFramework.RoutedMessaging;assembly=Caliburn.PresentationFramework&amp;quot;
             xmlns:d=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;
             xmlns:mc=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;
             mc:Ignorable=&amp;quot;d&amp;quot; x:Class=&amp;quot;ContactManager.Views.ContactListView&amp;quot;
             xmlns:local=&amp;quot;clr-namespace:ContactManager.Presenters&amp;quot;             
             Height=&amp;quot;400&amp;quot;
             Width=&amp;quot;750&amp;quot;
             d:DataContext=&amp;quot;{d:DesignInstance local:ContactListPresenter}&amp;quot;
             rm:Message.Attach=&amp;quot;[Event Loaded] = [Action LoadContacts]&amp;quot;&amp;gt;

&amp;lt;!-- omitted for brevity --&amp;gt;
&amp;lt;/UserControl&amp;gt;&lt;/pre&gt;

&lt;p&gt;Wow, that’s a lot of namespaces. We’re only concerned with two lines though. The first one is&lt;/p&gt;

&lt;pre&gt;    xmlns:local=&amp;quot;clr-namespace:ContactManager.Presenters&amp;quot; &lt;/pre&gt;

&lt;p&gt;This also use to reference our local type in XAML.&lt;/p&gt;

&lt;p&gt;The second is&lt;/p&gt;

&lt;pre&gt;    d:DataContext=&amp;quot;{d:DesignInstance local:ContactListPresenter}&amp;quot; &lt;/pre&gt;

&lt;p&gt;This tells Blend that we want to use the ContactListPresenter for our design-time data context. However, this doesn’t create an instance of the type and we won’t see any data appear in Blend. Instead, this tells Blend what the “shape” of our data context is. In other words, Blend uses reflection to learn about the properties so that it can show us a lot more in the data binding editor. &lt;/p&gt;

&lt;p&gt;Let’s open that up again.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="a really useful data binding engine" border="0" alt="a really useful data binding engine" src="http://devlicious.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/christopher_5F00_bennage/image_5F00_6463D0CD.png" width="616" height="578" /&gt; &lt;/p&gt;

&lt;p&gt;Much less depressing. This also means that when I drill down and edit the ItemTemplate, Blend is aware of the data context and I get support for bindings there as well. Booya.&lt;/p&gt;

&lt;h3&gt;Getting Some Sample Data&lt;/h3&gt;

&lt;p&gt;I said earlier that d:DesignInstance does not create an instance of the specified type. You can make it create an instance though by setting the IsDesignTimeCreatable property to true. Now, we don’t want to do this with ContactListPresenter because it has a couple of dependencies injected into it’s constructor. So in order to provide a little sample data for the designer, I’ve created a new class to use as my design-time viewmodel:&lt;/p&gt;

&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public class SampleContactListPresenter
{
    public List&amp;lt;Contact&amp;gt; Contacts {get;set;}    
    
    public SampleContactListPresenter()
    {
        Contacts = new List&amp;lt;Contact&amp;gt;
        {
            new Contact{ FirstName=&amp;quot;Ford&amp;quot;, LastName=&amp;quot;Prefect&amp;quot;},
            new Contact{ FirstName=&amp;quot;Author&amp;quot;, LastName=&amp;quot;Dent&amp;quot;},
        };
    }
    
}&lt;/pre&gt;

&lt;p&gt;I could have derived this from ContactListPresenter, but that would have been a lot of unnecessary work. A better choice would be for my design-time viewmodel and my real viewmodel to implement a common interface. That would help them maintain the same shape. (An even better choice would be to use d:DesignData, but that’s another post). &lt;/p&gt;

&lt;p&gt;In this case, I decided to keep it really simple.&lt;/p&gt;

&lt;p&gt;After adding this class, I need to change the line in my XAML that creates the design-time data context to this:&lt;/p&gt;

&lt;p&gt;d:DataContext=&amp;quot;{d:DesignInstance local:SampleContactListPresenter, IsDesignTimeCreatable=True}&amp;quot;&lt;/p&gt;

&lt;p&gt;and now I can see some sample data in Blend:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Some actual, real not-so real or actual data." border="0" alt="Some actual, real not-so real or actual data." src="http://devlicious.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/christopher_5F00_bennage/image_5F00_787CED56.png" width="409" height="438" /&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;N.B. I discovered after I worked through this that the ContactListPresenter in the trunk doesn’t get along well with d:DesignInstance. I haven’t isolated the exact problem yet, but you’ll&amp;#160; need to modify the class if you want to follow along with this tutorial. Change the class so that it is only inheriting from ScreenConductor&amp;lt;IContactDetailsPresenter&amp;gt;:&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;em&gt;public class ContactListPresenter : ScreenConductor&amp;lt;IContactDetailsPresenter&amp;gt;, IContactListPresenter&lt;/em&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=56325" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/WPF/default.aspx">WPF</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/data+binding/default.aspx">data binding</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/UI/default.aspx">UI</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/Blend/default.aspx">Blend</category></item><item><title>Exploring Design-Time Data in Blend 4</title><link>http://devlicious.com/blogs/christopher_bennage/archive/2010/03/26/exploring-design-time-data-in-blend-4.aspx</link><pubDate>Fri, 26 Mar 2010 20:26:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:56294</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I’ve gleaned most of what I have learned about this topic from &lt;a title="Unni" href="http://blogs.msdn.com/unnir/archive/2009/07/12/introducing-sample-data-for-developers.aspx" target="_blank"&gt;Unni&lt;/a&gt;, &lt;a title="Karl Shifflett" href="http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/" target="_blank"&gt;Karl&lt;/a&gt;, and &lt;a title="Jack Ukleja" href="http://jack.ukleja.com/ddesigninstance-in-depth/" target="_blank"&gt;Jack&lt;/a&gt;. I recommend checking out their posts for additional information.&lt;/p&gt;  &lt;h3&gt;d:DataContext&lt;/h3&gt;  &lt;p&gt;The d:DataContext attribute allows you to establish a data context that is only for design time. You can use it the same way (more or less) that you use the real DataContext. Two options are available for providing data for the design-time data context. They are the markup extensions d:DesignInstance and d:DesignData.&lt;/p&gt;  &lt;h3&gt;{d:DesignInstance}&lt;/h3&gt;  &lt;p&gt;This new markup extension has three properties:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="4" width="500"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Type&lt;/td&gt;        &lt;td valign="top" width="400"&gt;The type of the instance that we would like to use for design-time data context. This is the default property, so you can omit it and just provide the type value.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;IsDesignTimeCreatable&lt;/td&gt;        &lt;td valign="top" width="400"&gt;&lt;strong&gt;True&lt;/strong&gt;: create an instance of the type and use it for the design-time data.           &lt;br /&gt;&lt;strong&gt;False&lt;/strong&gt;: reflect over the type so that the data binding expression builder is aware of the members.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;CreateList&lt;/td&gt;        &lt;td valign="top" width="400"&gt;Do we want a list of instances or a single instance?&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;This is mostly for giving you &lt;em&gt;the shape&lt;/em&gt; of the data that you are binding to. Though you will see the actual property values at design-time, so you can create a new type with the same shape as your real viewmodel, but with data to use for design-time (perhaps by interface, perhaps by subclassing). This approach can be tedious though, especially in cases where you need lists in design-time data.&lt;/p&gt;  &lt;h3&gt;{d:DesignData}&lt;/h3&gt;  &lt;p&gt;This markup extension has a single property, Source, that points to a xaml file describing the design-time data. The best way to create this xaml file is using Blend. Blend can automatically generate &lt;a href="http://www.lipsum.com/" target="_blank"&gt;Lorem Ipsum&lt;/a&gt; for your viewmodels (which is kinda cool). I’ll show you how to do that in a follow-up post.&lt;/p&gt;  &lt;p&gt;N.B. While everything works brilliantly in Blend, I wasn’t able to get d:DesignData to work for Silverlight in Cider (Visual Studio 2010), it complained about the leading forward slash (/) but still didn’t work when I removed it. However, removing the forward slash broke it for Blend.&lt;/p&gt;  &lt;h3&gt;A Quick Example&lt;/h3&gt;  &lt;pre class="xml:nogutter:nocontrols" name="code"&gt;&amp;lt;UserControl x:Class=&amp;quot;DesignTimeTest.MyView&amp;quot;
             xmlns=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
             xmlns:x=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
             xmlns:d=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;
             xmlns:mc=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;
             mc:Ignorable=&amp;quot;d&amp;quot;
             xmlns:local=&amp;quot;clr-namespace:DesignTimeTest&amp;quot;
             d:DesignHeight=&amp;quot;300&amp;quot;
             d:DesignWidth=&amp;quot;400&amp;quot;
             d:DataContext=&amp;quot;{d:DesignInstance local:MyViewModel, IsDesignTimeCreatable=True}&amp;quot; /&amp;gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://devlicio.us/blogs/christopher_bennage/archive/2010/03/29/d-designinstance-in-blend-4.aspx" target="_blank"&gt;More on d:DesignInstance&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=56294" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/WPF/default.aspx">WPF</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/data+binding/default.aspx">data binding</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/UI/default.aspx">UI</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/mvvm/default.aspx">mvvm</category><category domain="http://devlicious.com/blogs/christopher_bennage/archive/tags/Blend/default.aspx">Blend</category></item></channel></rss>