<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>davidpoll.com &#187; Silverlight Toolkit</title>
	<atom:link href="http://www.davidpoll.com/tag/silverlight-toolkit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidpoll.com</link>
	<description>Software development and other goofy geeky goodness.</description>
	<lastBuildDate>Sat, 18 Jun 2011 22:03:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Making printing easier in Silverlight 4</title>
		<link>http://www.davidpoll.com/2010/04/16/making-printing-easier-in-silverlight-4/</link>
		<comments>http://www.davidpoll.com/2010/04/16/making-printing-easier-in-silverlight-4/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 23:35:21 +0000</pubDate>
		<dc:creator>david.poll</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Printing]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Silverlight and Beyond (SLaB)]]></category>
		<category><![CDATA[Silverlight Toolkit]]></category>

		<guid isPermaLink="false">http://www.davidpoll.com/2010/04/16/making-printing-easier-in-silverlight-4/</guid>
		<description><![CDATA[Well, what an exciting week!&#160; First Visual Studio 2010 is released, followed by Silverlight 4 yesterday!&#160; Consequently, I was inspired to post about something new!&#160; I’ve been spending some time looking at the new printing feature in Silverlight 4, and while on the surface it looks like a pretty simple and lower-level set of APIs, [...]]]></description>
			<content:encoded><![CDATA[<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_start --><p>Well, what an exciting week!&#160; First Visual Studio 2010 is released, followed by <a href="http://www.silverlight.net/getstarted/silverlight-4/">Silverlight 4</a> yesterday!&#160; Consequently, I was inspired to post about something new!&#160; I’ve been spending some time looking at the new printing feature in Silverlight 4, and while on the surface it looks like a pretty simple and lower-level set of APIs, it’s possible to build rich frameworks on top of them for accomplishing common printing tasks.&#160; In this post, I’ll take a look at an attempt I made (and added to <a href="http://www.davidpoll.com/downloads-and-samples/#SLaB">SLaB</a>) at building such a higher-level API over printing that makes printing collections of data easier.</p>
<p>Specifically, I’ve been building a CollectionPrinter control – a control that paginates, previews, and prints collections in a template-driven, designer-friendly way.&#160; With this control, printing a collection can be entirely XAML-based and code-free!</p>
<p>For example, the image below shows some sample data (courtesy of the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=88484825-1b3c-4e8c-8b14-b05d025e1541&amp;displaylang=en">Blend 4 RC</a>) within a DataGrid being printed across multiple pages.</p>
<p><a href="http://www.davidpoll.com/Samples/Download/Short%20DataGrid%20Document.pdf"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="A printed collection." border="0" alt="A printed collection." src="http://www.davidpoll.com/wp-content/uploads/2010/04/image.png" width="644" height="404" /></a></p>
<p>And you can see it in action by clicking <a href="http://www.davidpoll.com/Samples/SLaB/#/Views/SitemapPage.xaml?sitemapname=PrintingSitemap">here</a>.</p>
<p>Of course, this is just an example of how one might want to build such a library, but it hopefully inspires some ideas you all might have around printing!&#160; I’d love to know what you think! </p>
<h3>So, how does printing work?</h3>
<p>The printing APIs in Silverlight 4 center around the <a href="http://msdn.microsoft.com/en-us/library/system.windows.printing.printdocument(VS.96).aspx">PrintDocument</a> class.&#160; SilverlightShow has a great <a href="http://www.silverlightshow.net/items/A-look-at-the-Printing-API-in-Silverlight-4.aspx">article on the basics of using this class</a>.&#160; Basically, the workflow for printing in Silverlight 4 is the following:</p>
<ol>
<li>Create a new PrintDocument </li>
<li>Attach a handler to the PrintDocument.PrintPage event, which will be called for each page you choose to print </li>
<li>Call PrintDocument.Print(), passing in a document name (which will appear in the print spooler, for example) </li>
<li>The user is prompted to print </li>
<li>In each PrintPage call:
<ol>
<li>If you have content ready to print:
<ol>
<li>Choose some UI to print to the page and set the PageVisual in the PrintPageEventArgs </li>
<li>If there are more pages to print, set HasMorePages on PrintPageEventPargs to true </li>
</ol>
</li>
<li>If you’re not ready to print yet:
<ol>
<li>Set PageVisual to null </li>
<li>Set HasMorePages to true (you will be called back after a short delay for the content – up to 8 attempts will be made to print a page before printing fails) </li>
</ol>
</li>
</ol>
</li>
</ol>
<p>And that’s it!&#160; For each page, Silverlight will render a bitmap of the visuals you provided in PageVisual (just like with <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap(VS.95).aspx">WriteableBitmap</a>), then send this bitmap to the printer.</p>
<h3>That all seems pretty straightforward… why do I need anything more?</h3>
<p>Well, it<em> is</em> pretty straightforward.&#160; However, there’s a fair amount of work that you would have to do in order to create a coherently printed document:</p>
<ul>
<li><strong>Pagination</strong> – in order to figure out which items to print on each page, you will need to add items to the page, searching for the item that would flow beyond the end of the page.&#160; This involves measuring/arranging pages and expanding templates, keeping track of the items you’ve printed so far, handling cases where items are too big for the page, and so on. </li>
<li><strong>Page layout</strong> – it’s very common to want headers/footers on your pages, which further complicates the pagination process. </li>
<li><strong>Page context</strong> – when printing a particular page, you often want some additional context, such as which items are on the page, the page number, the total number of pages (so that you can print “Page 1 / 10”, whether this is the first or last page, etc.&#160; This context must be calculated and tracked, and is difficult when the printing process is progressive as with the above API (e.g. how can I print the first page without first having rendered all of the pages, so that I know what the total page count will be?) </li>
<li><strong>Dealing with content not in the visual tree</strong> – often, you want to print UI directly from the visual tree, which is much easier.&#160; But when printing controls that are <strong><em>NOT</em></strong> in the visual tree, life gets much more complicated.&#160; As with WriteableBitmap, controls used as the PageVisual don’t get a full visual tree pass, meaning that events like <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded(v=VS.95).aspx">FrameworkElement.Loaded</a> don’t get raised.&#160; Some controls use such events to do initialization, such as the charting controls in the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a>.&#160; In order to properly print arbitrary controls, it’s often helpful to actually place the control in the visual tree. </li>
<li><strong>Handling animations</strong> – many controls (again, such as the charting controls in the Silverlight Toolkit) have animations and transitions by default.&#160; For the charting controls, the data points being charted fade in by default.&#160; If you try to print this when the control is created, you’ll get a blank chart!&#160; One way to deal with this (if it’s the desired behavior) is to walk the visual tree, causing any running storyboard to <a href="http://msdn.microsoft.com/en-us/library/cc190726(VS.96).aspx">SkipToFill</a>. </li>
<li><strong>Print preview</strong> – while it’s not really possible to give a full preview of what a printed document will look like (because you can’t find out what the size/margins of the page will be before printing has started), one can approximate it and at least provide a hint as to what to expect when printing begins. </li>
<li><strong>Designability</strong> – Working with pages to print in Blend or Visual Studio’s designer can be somewhat difficult, especially if you need to write the code to handle the above cases. </li>
</ul>
<p>As you can see, there are a number of complicating factors when doing sophisticated printing, but these can be abstracted away if the domain you’re given is specific enough.&#160; In this case, we’re printing a collection, and we can use the items in the collection as the units between which we can break up pages.</p>
<h3>OK, cool, I think I get it.&#160; So, how does it work?</h3>
<p>You can use the CollectionPrinter much like an <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(VS.95).aspx">ItemsControl</a>.&#160; It’s fundamentally DataTemplate-driven.&#160; It has an ItemsSource and an ItemTemplate, but instead, you can specify a BodyTemplate (which, by default, is an ItemsControl <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).&#160; For headers, footers, and such, you can specify additional DataTemplates.</p>
<p>All of the DataTemplates are bound to a “CollectionPrintContext” which contains the following pieces of information:</p>
<ul>
<li><strong>CurrentItems</strong> – the set of items being printed on this page (this collection will be built up dynamically during printing/previewing) </li>
<li><strong>CurrentPage/CurrentPageIndex</strong> – the page number (1- or 0-based) being printed </li>
<li>First/Last items (so that you can print “Items 1-10” or “Aa – Aardvark” on each page):
<ul>
<li><strong>FirstItem/FirstItemIndex/FirstItemValue</strong> – the first item being printed on the page (1-based index, 0-based index, or actual value, respectively) </li>
<li><strong>LastItem/LastItemIndex/LastItemValue</strong> – the last item being printed on the page (1-based index, 0-based index, or actual value, respectively) </li>
</ul>
</li>
<li><strong>PageCount</strong> – the total number of pages to print (note: this is a nullable int value and may not be provided if there isn’t time to finish pre-rendering all pages before the PrintPage callback limit is reached) </li>
<li><strong>PageMargins</strong> – the margins of the page being printed </li>
<li><strong>PrintableArea</strong> – the size of the space in which the page can be printed </li>
</ul>
<p>The hope is that with these pieces of information to bind to, you can print rich pages for your users.&#160; The rest is just creative use of bindings <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To kick off printing, either call the Print() method on the CollectionPrinter or bind a button to the PrintCommand property on the CollectionPrinter for completely code-free printing!</p>
<p>The CollectionPrinter will attempt to address all of the issues above, spawning invisible popups to ensure that the UI actually has a moment to be in the visual tree, walking the visual tree to skip animations to fill, and most importantly paginating all of the items in the ItemsSource.</p>
<h3>Let’s see it in action!</h3>
<h4>Using an ItemTemplate</h4>
<p>The most basic case for printing with the CollectionPrinter is to use an ItemTemplate, just as you would with an ItemsControl.&#160; The CollectionPrinter will happily handle cases where items are irregularly sized, so be as rich as you’d like!</p>
<p>For example:</p>
<p><a href="http://www.davidpoll.com/Samples/SLaB/#pack://siteoforigin:,,ScratchPrintingProject.xap/ScratchPrintingProject;component/ItemTemplatePrinting.xaml"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="ItemTemplate-based Printing" border="0" alt="ItemTemplate-based Printing" src="http://www.davidpoll.com/wp-content/uploads/2010/04/image1.png" width="644" height="425" /></a> </p>
<p>The XAML style for this CollectionPrinter is as follows:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Style </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;PrintStyle&quot;
        </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;SLaB:CollectionPrinter&quot;&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;ItemTemplate&quot;&gt;
        &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">Border </span><span style="color: red">BorderThickness</span><span style="color: blue">=&quot;1&quot;
                        </span><span style="color: red">BorderBrush</span><span style="color: blue">=&quot;Gray&quot;&gt;
                    &lt;</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;
                        &lt;</span><span style="color: #a31515">Grid.Resources</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">Style </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;TextBlock&quot;&gt;
                                &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;FontSize&quot;
                                        </span><span style="color: red">Value</span><span style="color: blue">=&quot;12&quot; /&gt;
                            &lt;/</span><span style="color: #a31515">Style</span><span style="color: blue">&gt;
                        &lt;/</span><span style="color: #a31515">Grid.Resources</span><span style="color: blue">&gt;
                        &lt;</span><span style="color: #a31515">Grid.ColumnDefinitions</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">ColumnDefinition </span><span style="color: blue">/&gt;
                            &lt;</span><span style="color: #a31515">ColumnDefinition </span><span style="color: blue">/&gt;
                        &lt;/</span><span style="color: #a31515">Grid.ColumnDefinitions</span><span style="color: blue">&gt;
                        &lt;</span><span style="color: #a31515">StackPanel </span><span style="color: red">Grid.Column</span><span style="color: blue">=&quot;0&quot;
                                    </span><span style="color: red">Margin</span><span style="color: blue">=&quot;10&quot;&gt;
                            &lt;</span><span style="color: #a31515">Image </span><span style="color: red">Source</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Photo</span><span style="color: blue">}&quot;
                                   </span><span style="color: red">Height</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Age</span><span style="color: blue">}&quot;
                                   </span><span style="color: red">Width</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Age</span><span style="color: blue">}&quot;
                                   </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Left&quot; /&gt;
                            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Name</span><span style="color: blue">, </span><span style="color: red">StringFormat</span><span style="color: blue">='Name: {0}'}&quot; /&gt;
                            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Age</span><span style="color: blue">, </span><span style="color: red">StringFormat</span><span style="color: blue">='Age: {0}'}&quot; /&gt;
                            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Address</span><span style="color: blue">, </span><span style="color: red">StringFormat</span><span style="color: blue">='Address: {0}'}&quot;
                                       </span><span style="color: red">TextWrapping</span><span style="color: blue">=&quot;Wrap&quot; /&gt;
                        &lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
                        &lt;</span><span style="color: #a31515">toolkit</span><span style="color: blue">:</span><span style="color: #a31515">Chart </span><span style="color: red">Grid.Column</span><span style="color: blue">=&quot;1&quot;
                                       </span><span style="color: red">Title</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Name</span><span style="color: blue">}&quot;&gt;
                            &lt;</span><span style="color: #a31515">toolkit</span><span style="color: blue">:</span><span style="color: #a31515">ScatterSeries </span><span style="color: red">DependentValuePath</span><span style="color: blue">=&quot;X&quot;
                                                   </span><span style="color: red">IndependentValuePath</span><span style="color: blue">=&quot;Y&quot;
                                                   </span><span style="color: red">Title</span><span style="color: blue">=&quot;Values&quot;
                                                   </span><span style="color: red">ItemsSource</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Values</span><span style="color: blue">}&quot; /&gt;
                        &lt;/</span><span style="color: #a31515">toolkit</span><span style="color: blue">:</span><span style="color: #a31515">Chart</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;
                &lt;/</span><span style="color: #a31515">Border</span><span style="color: blue">&gt;
            &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;HeaderTemplate&quot;&gt;
        &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt; ... </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;FooterTemplate&quot;&gt;
        &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt; ... </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Style</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;printer&quot;
                        </span><span style="color: red">Style</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">StaticResource </span><span style="color: red">PrintStyle</span><span style="color: blue">}&quot; /&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>All it takes to print based on this style is setting the ItemsSource (in this case to some Blend sample data) and binding a button to the PrintCommand on the control.&#160; In my header and footer templates, I have controls that are bound to the CollectionPrintContext, and whose visibilities are determined by binding to things like IsFirstPage (see the source included in the SLaB download for more details).</p>
<h4>Changing the BodyTemplate (and printing a DataGrid)</h4>
<p>Now, that’s all wonderful if all I want to do is print something that looks like an ItemsControl.&#160; But what can we do if we want to print a DataGrid (with column headers, etc.)?&#160; Well, the CollectionPrinter doesn’t depend on being given an ItemsControl at any point.&#160; Instead, it determines pagination based upon the DesiredSize of its content after measuring/arranging.&#160; As a result, anything that grows as its bound content is changed will work with the CollectionPrinter.&#160; You can easily print a DataGrid in this way, as I’ve done below:</p>
<p><a href="http://www.davidpoll.com/Samples/SLaB/#pack://siteoforigin:,,ScratchPrintingProject.xap/ScratchPrintingProject;component/PrintingPage.xaml"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="DataGrid-based Printing" border="0" alt="DataGrid-based Printing" src="http://www.davidpoll.com/wp-content/uploads/2010/04/image2.png" width="644" height="432" /></a>And the XAML barely changes from what you see above.&#160; In this case, instead of using an ItemTemplate, I set a BodyTemplate, which is a DataGrid (with vertical scrolling disabled) with a variety of columns.&#160; In this case, I’ll specify the DataTemplates inline:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">sdk</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&quot;
                        </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">chartingToolkit</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit&quot;
</span><span style="color: blue">                        </span><span style="color: red">xmlns</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                        </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">x</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
</span><span style="color: blue">                        </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">SLaB</span><span style="color: blue">=&quot;http://www.davidpoll.com/SLaB</span><span style="color: blue">&quot;&gt;
    &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.HeaderTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt; ...</span><span style="color: blue"> &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.HeaderTemplate</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.FooterTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt; ...</span><span style="color: blue"> &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.FooterTemplate</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.BodyTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGrid </span><span style="color: red">ItemsSource</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">CurrentItems</span><span style="color: blue">}&quot;
                          </span><span style="color: red">AutoGenerateColumns</span><span style="color: blue">=&quot;False&quot;
                          </span><span style="color: red">VerticalScrollBarVisibility</span><span style="color: blue">=&quot;Disabled&quot;&gt;
                &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGrid.Columns</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTextColumn </span><span style="color: red">Binding</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Name</span><span style="color: blue">}&quot;
                                            </span><span style="color: red">Header</span><span style="color: blue">=&quot;Name&quot; /&gt;
                    &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTextColumn </span><span style="color: red">Binding</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Address</span><span style="color: blue">}&quot;
                                            </span><span style="color: red">Header</span><span style="color: blue">=&quot;Address&quot; /&gt;
                    &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTextColumn </span><span style="color: red">Binding</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Age</span><span style="color: blue">}&quot;
                                            </span><span style="color: red">Header</span><span style="color: blue">=&quot;Age&quot; /&gt;
                    &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn </span><span style="color: red">Header</span><span style="color: blue">=&quot;Image&quot;
                                                </span><span style="color: red">IsReadOnly</span><span style="color: blue">=&quot;True&quot;&gt;
                        &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn.CellTemplate</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">Image </span><span style="color: red">Source</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Photo</span><span style="color: blue">}&quot;
                                       </span><span style="color: red">Height</span><span style="color: blue">=&quot;50&quot;
                                       </span><span style="color: red">Width</span><span style="color: blue">=&quot;50&quot; /&gt;
                            &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                        &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn.CellTemplate</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn </span><span style="color: red">Header</span><span style="color: blue">=&quot;Values&quot;
                                                </span><span style="color: red">Width</span><span style="color: blue">=&quot;*&quot;
                                                </span><span style="color: red">IsReadOnly</span><span style="color: blue">=&quot;True&quot;&gt;
                        &lt;</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn.CellTemplate</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">chartingToolkit</span><span style="color: blue">:</span><span style="color: #a31515">Chart </span><span style="color: red">Title</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Name</span><span style="color: blue">}&quot;&gt;
                                    &lt;</span><span style="color: #a31515">chartingToolkit</span><span style="color: blue">:</span><span style="color: #a31515">ScatterSeries </span><span style="color: red">DependentValuePath</span><span style="color: blue">=&quot;X&quot;
                                                                   </span><span style="color: red">IndependentValuePath</span><span style="color: blue">=&quot;Y&quot;
                                                                   </span><span style="color: red">Title</span><span style="color: blue">=&quot;Values&quot;
                                                                   </span><span style="color: red">ItemsSource</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">Values</span><span style="color: blue">}&quot; /&gt;
                                &lt;/</span><span style="color: #a31515">chartingToolkit</span><span style="color: blue">:</span><span style="color: #a31515">Chart</span><span style="color: blue">&gt;
                            &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                        &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn.CellTemplate</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGridTemplateColumn</span><span style="color: blue">&gt;
                &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGrid.Columns</span><span style="color: blue">&gt;
            &lt;/</span><span style="color: #a31515">sdk</span><span style="color: blue">:</span><span style="color: #a31515">DataGrid</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.BodyTemplate</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter</span><span style="color: blue">&gt;

</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h4>Explicitly providing individual pages</h4>
<p>You can also use the CollectionPrinter for printing multiple pages explicitly (automatically dealing with all of the PrintPage callbacks, running animations, etc.) by providing a series of DataTemplates (one for each page) as the ItemsSource, changing the BodyTemplate to a ContentControl, and setting the maximum number of items to print per page to 1.&#160; For example:</p>
<p><a href="http://www.davidpoll.com/Samples/SLaB/#pack://siteoforigin:,,ScratchPrintingProject.xap/ScratchPrintingProject;component/ItemTemplatePrinting.xaml"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Individual Pages" border="0" alt="Individual Pages" src="http://www.davidpoll.com/wp-content/uploads/2010/04/IndividualPages.png" width="644" height="419" /></a> </p>
<p>And, of course, the corresponding XAML:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">ObservableObjectCollection </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;Pages&quot;&gt;
    &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">FontWeight</span><span style="color: blue">=&quot;Bold&quot;
                        </span><span style="color: red">FontSize</span><span style="color: blue">=&quot;20&quot;&gt;</span><span style="color: #a31515">Lorem Ipsum</span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">TextWrapping</span><span style="color: blue">=&quot;Wrap&quot;
                        </span><span style="color: red">Margin</span><span style="color: blue">=&quot;10&quot;
                        </span><span style="color: red">xml</span><span style="color: blue">:</span><span style="color: red">space</span><span style="color: blue">=&quot;preserve&quot;&gt;</span><span style="color: #a31515">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ultrices cursus tortor ac egestas. Pellentesque semper lobortis enim, vel imperdiet dolor vehicula ac. Suspendisse auctor tempus molestie. Cras pulvinar sagittis libero, vel pretium ipsum consectetur sit amet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum feugiat nunc eget ante euismod ac facilisis velit aliquet. Vestibulum eget nulla magna, eget scelerisque ligula. Cras nec nisi faucibus leo fermentum euismod eu vel lacus. Etiam lacus massa, pulvinar id tempor eget, varius at lorem. Praesent venenatis nisi ac ipsum facilisis at suscipit magna sollicitudin. Phasellus placerat imperdiet hendrerit. Nulla ac risus velit. Sed orci lorem, imperdiet vel ultrices et, viverra ut leo. Mauris feugiat, diam eget mollis tempus, est leo pellentesque risus, vitae lacinia ante felis hendrerit elit. 

Suspendisse potenti. Donec dui justo, ultrices quis condimentum vel, bibendum vel nisi. Pellentesque suscipit fermentum dui vel sodales. Nulla vitae tortor vel orci posuere vestibulum. Curabitur non lacus quam. Nulla sit amet tempor libero. Integer dictum lectus ut sem adipiscing vitae fringilla felis accumsan. Mauris ut risus felis, ut pulvinar quam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Integer fermentum, turpis sit amet tincidunt fermentum, metus mauris bibendum nisi, nec tincidunt purus enim nec nisi. Sed faucibus congue ultricies. Maecenas sed lorem id sem ornare imperdiet ut vitae libero. Curabitur mi diam, ornare sit amet dignissim eu, imperdiet sed nibh. Donec ultrices libero sed ipsum sollicitudin in dapibus elit rutrum. Nulla egestas tempus est, nec semper lacus sodales vel. Quisque consectetur turpis nunc, eu pretium felis. Etiam non adipiscing elit. 

Cras sit amet volutpat metus. Nunc eu augue eu urna placerat adipiscing in vel lacus. Etiam auctor orci nec dui adipiscing non viverra nisl gravida. In lacinia venenatis lobortis. Vestibulum dignissim, dolor ut feugiat ultricies, eros odio adipiscing augue, quis congue turpis augue quis mauris. Aliquam at ligula sem. Aenean eget arcu ac odio eleifend convallis. Aenean eu tellus ac eros placerat aliquam. Nam consectetur neque sed massa accumsan mollis. Pellentesque in mi erat, eget tristique elit. Praesent mattis magna sed est placerat bibendum venenatis nulla facilisis. Duis nec mollis nisi. Vestibulum et eros vitae felis vestibulum scelerisque. Donec venenatis, nulla vel rutrum tempus, purus nulla feugiat felis, eu semper diam nibh tincidunt metus. 

Praesent venenatis aliquet vulputate. In suscipit, nulla ut pulvinar ullamcorper, diam ligula sagittis enim, fermentum tempus nunc neque sed nisi. Vivamus aliquam rutrum scelerisque. Phasellus suscipit, quam sed suscipit pretium, massa nunc elementum lectus, et adipiscing arcu turpis sed dolor. Suspendisse potenti. Proin nisl mauris, sodales tincidunt ultricies sed, placerat quis enim. Nulla elementum nunc vel sapien venenatis venenatis vel eu ligula. Sed vitae erat ante. Etiam nec sapien nec sapien sagittis hendrerit. Duis at odio dolor. Sed condimentum euismod felis, ut congue dolor luctus quis. Duis non tellus enim. Quisque quis odio erat. Nulla nulla mi, dapibus ut euismod ut, adipiscing nec lacus. Aliquam faucibus dui at est accumsan ut laoreet erat pellentesque. Nullam id malesuada tellus. Donec et ligula tincidunt metus rutrum pretium. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; 

Sed lacinia dignissim scelerisque. Duis pharetra elit et nisl euismod viverra. Nam semper, purus ut luctus tincidunt, eros nisi aliquam nunc, non semper lorem enim sit amet augue. Vestibulum adipiscing tortor a magna tristique fringilla. Etiam porta volutpat odio, eu posuere velit mollis non. Mauris ut arcu quis lectus dapibus condimentum. Pellentesque non bibendum nisi. In hac habitasse platea dictumst. Maecenas laoreet lorem ut sem pellentesque id facilisis nibh pulvinar. Vivamus tempus erat placerat diam condimentum ac bibendum felis egestas. Quisque mollis hendrerit risus, ac euismod metus dapibus nec. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus quis ipsum euismod dolor pharetra viverra eget a augue. Morbi lorem enim, porta ut congue quis, pretium et enim.
            </span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">Grid.RowDefinitions</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">RowDefinition </span><span style="color: red">Height</span><span style="color: blue">=&quot;Auto&quot; /&gt;
                &lt;</span><span style="color: #a31515">RowDefinition </span><span style="color: red">Height</span><span style="color: blue">=&quot;*&quot; /&gt;
            &lt;/</span><span style="color: #a31515">Grid.RowDefinitions</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">FontWeight</span><span style="color: blue">=&quot;Bold&quot;
                        </span><span style="color: red">FontSize</span><span style="color: blue">=&quot;20&quot;&gt;</span><span style="color: #a31515">Lorem Ipsum</span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">Image </span><span style="color: red">Grid.Row</span><span style="color: blue">=&quot;1&quot;
                    </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Center&quot;
                    </span><span style="color: red">VerticalAlignment</span><span style="color: blue">=&quot;Center&quot;
                    </span><span style="color: red">Source</span><span style="color: blue">=&quot;/ScratchPrintingProject;component/SLaB Logo.png&quot; /&gt;
        &lt;/</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">ObservableObjectCollection</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Style </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;PrintStyle&quot;
        </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;SLaB:CollectionPrinter&quot;&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;MaximumItemsPerPage&quot;
            </span><span style="color: red">Value</span><span style="color: blue">=&quot;1&quot; /&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;BodyTemplate&quot;&gt;
        &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">ContentControl </span><span style="color: red">ContentTemplate</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">CurrentItems</span><span style="color: blue">[</span>0<span style="color: blue">]}&quot;
                                </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">=&quot;Stretch&quot;
                                </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">=&quot;Stretch&quot; /&gt;
            &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&quot;HeaderTemplate&quot;&gt;
        &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt; ...</span><span style="color: blue"> &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Style</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;printer&quot;
                        </span><span style="color: red">ItemsSource</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">StaticResource </span><span style="color: red">Pages</span><span style="color: blue">}&quot;
                        </span><span style="color: red">Style</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">StaticResource </span><span style="color: red">PrintStyle</span><span style="color: blue">}&quot; /&gt;
</span></pre>
<p>Above, I have two pages: one with a bunch of text, and one with a large image on it.&#160; I still get all of the context for my headers/footers, and can use this as a way to get the benefits of using the CollectionPrinter without being locked into an ItemsControl-like behavior.</p>
<h3></h3>
<h3></h3>
<h4>Headers and Footers</h4>
<p>Creating Headers and Footers for each page is also really easy with the CollectionPrinter – just specify a HeaderTemplate or a FooterTemplate.&#160; These DataTemplates are bound to the CollectionPrintContext, which you can use to generate your header and footer info.&#160; I use some ValueConverters (in one of the SLaB libraries, if you’d like to reuse them) to conditionalize Visibility of a title for a document to the first page, and to change colors/text for the other pages.&#160; I also use the FirstItemValue/LastItemValue to provide a “Aa – Aardvark” (like you’d find in a dictionary) footer on each page.</p>
<p>For example, the sample pages above use the following XAML for their header/footers:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.HeaderTemplate</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">StackPanel </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Stretch&quot;&gt;
            &lt;</span><span style="color: #a31515">StackPanel.Resources</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">BoolConverter </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;BoolConverter&quot; /&gt;
            &lt;/</span><span style="color: #a31515">StackPanel.Resources</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">StackPanel </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Right&quot;
                        </span><span style="color: red">Orientation</span><span style="color: blue">=&quot;Horizontal&quot;&gt;
                &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">CurrentPage</span><span style="color: blue">, </span><span style="color: red">StringFormat</span><span style="color: blue">='{}Page {0} '}&quot; /&gt;
                &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">PageCount</span><span style="color: blue">, </span><span style="color: red">StringFormat</span><span style="color: blue">='{}/ {0}'}&quot; /&gt;
            &lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Center&quot;
                        </span><span style="color: red">Visibility</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">IsFirstPage</span><span style="color: blue">, </span><span style="color: red">Converter</span><span style="color: blue">={</span><span style="color: #a31515">StaticResource </span><span style="color: red">BoolConverter</span><span style="color: blue">}}&quot;
                        </span><span style="color: red">FontSize</span><span style="color: blue">=&quot;32&quot;&gt;</span><span style="color: #a31515">This is a test document!</span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Center&quot;
                        </span><span style="color: red">Visibility</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">IsLastPage</span><span style="color: blue">, </span><span style="color: red">Converter</span><span style="color: blue">={</span><span style="color: #a31515">StaticResource </span><span style="color: red">BoolConverter</span><span style="color: blue">}}&quot;
                        </span><span style="color: red">FontSize</span><span style="color: blue">=&quot;16&quot;&gt;</span><span style="color: #a31515">This is the last page!</span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Center&quot;
                        </span><span style="color: red">FontSize</span><span style="color: blue">=&quot;14&quot;&gt;
                &lt;</span><span style="color: #a31515">TextBlock.Foreground</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">Binding </span><span style="color: red">Path</span><span style="color: blue">=&quot;CurrentPage&quot;&gt;
                        &lt;</span><span style="color: #a31515">Binding.Converter</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Even</span><span style="color: blue">&gt;
                                    &lt;</span><span style="color: #a31515">SolidColorBrush </span><span style="color: red">Color</span><span style="color: blue">=&quot;Blue&quot; /&gt;
                                &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Even</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Odd</span><span style="color: blue">&gt;
                                    &lt;</span><span style="color: #a31515">SolidColorBrush </span><span style="color: red">Color</span><span style="color: blue">=&quot;Red&quot; /&gt;
                                &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Odd</span><span style="color: blue">&gt;
                            &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter</span><span style="color: blue">&gt;
                        &lt;/</span><span style="color: #a31515">Binding.Converter</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">Binding</span><span style="color: blue">&gt;
                &lt;/</span><span style="color: #a31515">TextBlock.Foreground</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">Binding </span><span style="color: red">Path</span><span style="color: blue">=&quot;CurrentPage&quot;
                                </span><span style="color: red">StringFormat</span><span style="color: blue">=&quot;This page is {0}&quot;&gt;
                        &lt;</span><span style="color: #a31515">Binding.Converter</span><span style="color: blue">&gt;
                            &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Even</span><span style="color: blue">&gt;
                                    </span><span style="color: #a31515">Even
                                </span><span style="color: blue">&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Even</span><span style="color: blue">&gt;
                                &lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Odd</span><span style="color: blue">&gt;
                                    </span><span style="color: #a31515">Odd
                                </span><span style="color: blue">&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter.Odd</span><span style="color: blue">&gt;
                            &lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">EvenOddConverter</span><span style="color: blue">&gt;
                        &lt;/</span><span style="color: #a31515">Binding.Converter</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">Binding</span><span style="color: blue">&gt;
                &lt;/</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;
            &lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.HeaderTemplate</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.FooterTemplate</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">StackPanel </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;Center&quot;
                    </span><span style="color: red">Orientation</span><span style="color: blue">=&quot;Horizontal&quot;&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">FirstItemValue</span><span style="color: blue">.Name}&quot; /&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot; - &quot; /&gt;
            &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">Binding </span><span style="color: red">LastItemValue</span><span style="color: blue">.Name}&quot; /&gt;
        &lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">CollectionPrinter.FooterTemplate</span><span style="color: blue">&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Nifty, isn’t it? <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>That’s a lot of XAML!&#160; Do I have to write all of that by hand?</h3>
<p>Of course not!&#160; In fact, with the CollectionPrinter, Blend gives you a nice little print preview (especially if you use it as the root of a XAML file) and lets you modify the various templates directly in Blend, inline with the rest of the page!&#160; You can adjust the “CurrentPageIndex” property to preview your CollectionPrinter configuration in the designer.</p>
<p><a href="http://www.davidpoll.com/wp-content/uploads/2010/04/image3.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Editing templates within Blend" border="0" alt="Editing templates within Blend" src="http://www.davidpoll.com/wp-content/uploads/2010/04/image_thumb.png" width="644" height="386" /></a> </p>
<p>To accomplish this in Blend:</p>
<ul>
<li>Select a CollectionPrinter on the designer (either making it the root of a XAML document or selecting it in your existing XAML) </li>
<li>Right-click the CollectionPrinter in the Objects and Timeline Window </li>
<li>Choose “Edit Additional Templates” </li>
<li>Choose a template to edit, and either edit a copy, edit the current template, or create an empty template. </li>
<li>Edit away!&#160; Drag/drop your printed content! </li>
</ul>
<h3></h3>
<h3>So, what’s it all add up to?</h3>
<p>It’s very common for business applications to want to print the data they’ve collected, and it’s not hard to see how that might be a chore.&#160; Silverlight 4 introduced the printing feature, which allows printing at a very low level, but it also allows a huge amount of freedom in determining exactly what gets rendered to the page.&#160; This freedom can make simple printing tasks a chore.&#160; My hope is that the CollectionPrinter helps demonstrate how one might build a general-purpose (but more constrained than the built-in API) printing API.&#160; I’d love to know what you think.&#160; For me, it was primarily an experiment to see how close I could come to making printing large data sets easy and designable.&#160; Does this come close to what you’d hope for?&#160; What would your ideal be?</p>
<h3>Sweet!&#160; Can I try it?!</h3>
<p>I’m glad you’re so enthused!&#160; I’m tempted to say no, but wouldn’t torture you like that <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .&#160; You can give it a shot using my latest build of <a href="http://www.davidpoll.com/downloads-and-samples/#SLaB">Silverlight and Beyond</a>.</p>
<p>You can see the live demos here (Requires Silverlight 4):</p>
<p><a href="http://www.davidpoll.com/Samples/SLaB/#/Views/SitemapPage.xaml?sitemapname=PrintingSitemap"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Printing samples" border="0" alt="Printing samples" src="http://www.davidpoll.com/wp-content/uploads/2010/04/image4.png" width="644" height="432" /></a></p>
<p>If you’d like to save time printing (yeah, it takes a while – it renders some pretty large bitmaps!), you can see some sample output files below.&#160; Notice that the “Long” documents don’t have the total page count displayed, since it took longer to pre-render all of the pages than the PrintPage retry limit would permit:</p>
<ul>
<li>ItemTemplate-based Document – <a href="http://www.davidpoll.com/Samples/Download/Short%20Template-based%20Document.pdf">Short</a> (PDF 7.5 MB), <a href="http://www.davidpoll.com/Samples/Download/Long%20Template-based%20Document.pdf">Long</a> (PDF 37.7 MB) </li>
<li>DataGrid-based Document – <a href="http://www.davidpoll.com/Samples/Download/Short%20DataGrid%20Document.pdf">Short</a> (PDF 7.9 MB), <a href="http://www.davidpoll.com/Samples/Download/Long%20DataGrid%20Document.pdf">Long</a> (PDF 37.0 MB) </li>
<li>Individual page-based Document – <a href="http://www.davidpoll.com/Samples/Download/Short%20Document.pdf">Download</a> (PDF 5.8 MB) </li>
</ul>
<p>Finally, some source code for you:</p>
<li><a href="http://www.davidpoll.com/Samples/SLaB/#/Views/SitemapPage.xaml?sitemapname=PrintingSitemap">Live Sample</a> (<a href="http://www.davidpoll.com/Download/SLaB/SLaBv0.5.zip">source &#8212; found in the SLaB v0.5 source under &quot;ScratchPrintingProject&quot;</a>) </li>
<p><a href="http://www.davidpoll.com/Download/SLaB/SLaBv0.5.zip">SLaB v0.5</a> (includes source, a sample app, some tests, and binaries) </p>
<ul>
<li>For the latest version, please check out SLaB on my <a href="http://www.davidpoll.com/downloads-and-samples/#SLaB">Downloads and Samples</a> page. </li>
<li>The v0.5 download of SLaB includes the following changes:
<ul>
<li>Updated for SL4 RTW </li>
<li>Added CollectionPrinter for printing collections of items </li>
<li>Fixed a bug with Sitemap-based controls that caused some pack Uris to be evaluated as &quot;equivalent&quot; (and thus highlighted) even when they were not </li>
<li>Added EvenOddConverter that allows you to select an arbitrary value based on whether the input value was even or odd </li>
<li>Other minor bugfixes </li>
</ul>
</li>
</ul>
<p>Enjoy, and let me know if you have any questions, thoughts, or ideas!</p>
<p><strong><em>Remember</em></strong>, SLaB is just a collection of the samples and experimental components I’ve been putting together so that they’re all in one place.&#160; I can’t make any guarantees about maintaining them, fixing bugs, not making breaking changes, etc., but you’re more than welcome to try them out, use them, and let them inspire your development (or show you what <em>not</em> to do if you really dislike something I’m doing!) <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
</p>
<h3>P.S. Do you know you talk to yourself in these blog posts?</h3>
<p>Yes, yes I do, Mr. “Person behind the headers of the sections in my blog posts who is actually me.”&#160; </p>
<p>&#160;</p>
<p><strong><em><u>UPDATE 4/25/2010:</u></em></strong> Thanks for the feedback in the comments on this post.&#160; I have updated the CollectionPrinter to help with the DataGrid AutoGenerateColumns issue reported by “mb”.&#160; It may still have problems if the ItemsSource is not an IEnumerable&lt;T&gt; (but rather just a plain IEnumerable), but I think this should cover the 90% case.</p>
<p>You can download the newest bits <a href="http://www.davidpoll.com/Download/SLaB/SLaBv0.6.zip">here</a>.&#160; I also added a nice little utility method to SLaB that helps get a MethodInfo using a compiled Expression (using the LINQ expression libraries).&#160; Please let me know if you encounter other issues!</p>
<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://www.davidpoll.com/2010/04/16/making-printing-easier-in-silverlight-4/feed/</wfw:commentRss>
		<slash:comments>111</slash:comments>
		</item>
		<item>
		<title>New in the Silverlight 4 RC: XAML Features</title>
		<link>http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/</link>
		<comments>http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 00:58:35 +0000</pubDate>
		<dc:creator>david.poll</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Silverlight 4 RC]]></category>
		<category><![CDATA[Silverlight and Beyond (SLaB)]]></category>
		<category><![CDATA[Silverlight Toolkit]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/</guid>
		<description><![CDATA[Today, the Silverlight 4 RC was announced and made available to the masses.&#160; But you may be asking yourself: what’s new since the beta?&#160; Well, I’d like to dive into one of the areas where a bunch of new work was done to improve the development experience – the XAML Parser.&#160; With Silverlight 4, we’ve [...]]]></description>
			<content:encoded><![CDATA[<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_start --><p>Today, the <a href="http://www.silverlight.net/getstarted/silverlight-4/">Silverlight 4 RC</a> was announced and made available to the masses.&#160; But you may be asking yourself: what’s new since the beta?&#160; Well, I’d like to dive into one of the areas where a bunch of new work was done to improve the development experience – the XAML Parser.&#160; With Silverlight 4, we’ve done a significant overhaul of the XAML Parser, allowing us to add new features and improve consistency within the platform and with WPF’s XAML support.</p>
<p>So, let’s take a quick walk through some of the new things you can do with Silverlight XAML as of the RC!&#160; This is by no means an exhaustive list, but it’s definitely some of the bigger items.</p>
<h3>Direct Content</h3>
<p>This is one of those small inconsistencies with WPF that people hit almost immediately when they try to write Silverlight XAML after moving over from the WPF world.&#160; Specifically, things like this now work in Silverlight:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Button</span><span style="color: blue">&gt;</span><span style="color: #a31515">Click me!</span><span style="color: blue">&lt;/</span><span style="color: #a31515">Button</span><span style="color: blue">&gt;
</span></pre>
<p>I know, I know – exciting, right!?&#160; In Silverlight 3, you had to use attribute syntax or explicitly surround “Click Me!” in a string, like this:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Button </span><span style="color: red">Content</span><span style="color: blue">=&quot;Click me!&quot; /&gt;
</span></pre>
<p>or…</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Button </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">sys</span><span style="color: blue">=&quot;clr-namespace:System;assembly=mscorlib&quot;&gt;
    &lt;</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">String</span><span style="color: blue">&gt;</span><span style="color: #a31515">Click me!</span><span style="color: blue">&lt;/</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">String</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Button</span><span style="color: blue">&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I know when I first made the move from WPF to Silverlight, this was among the most irksome and frustrating things about its XAML parser.&#160; With Silverlight 4, that frustration is gone, and I can enjoy direct content in my controls once again!</p>
<p>See the image below for more examples:</p>
<p><a href="http://www.davidpoll.com/wp-content/uploads/2010/03/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="A quick demo of direct content support in Silverlight 4 XAML." border="0" alt="A quick demo of direct content support in Silverlight 4 XAML." src="http://www.davidpoll.com/wp-content/uploads/2010/03/image_thumb3.png" width="644" height="437" /></a> </p>
<h3></h3>
<h3>xml:space=”preserve”</h3>
<p>In previous versions of Silverlight, the XAML parser was rather liberal about how it applied whitespace.&#160; In general, it didn’t discard extra whitespace as most XML parsers will.</p>
<p>For example, the following XAML:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;</span><span style="color: #a31515">This
Text
Is
On
Separate
Lines
    </span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
</span></pre>
<p>Looked like this:</p>
<p><a href="http://www.davidpoll.com/wp-content/uploads/2010/03/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Whitespace preservation in prior versions of Silverlight." border="0" alt="Whitespace preservation in prior versions of Silverlight." src="http://www.davidpoll.com/wp-content/uploads/2010/03/image_thumb1.png" width="67" height="104" /></a> </p>
<p>The XAML parser in Silverlight 4 has finally corrected this behavior, and has also added support for xml:space=”preserve”.</p>
<p>When you recompile your application with the Silverlight 4 RC, the same XAML will produce the following:</p>
<p><a href="http://www.davidpoll.com/wp-content/uploads/2010/03/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.davidpoll.com/wp-content/uploads/2010/03/image_thumb2.png" width="179" height="20" /></a>&#160;</p>
<p>To get back the old behavior, add xml:space=”preserve” to your XAML:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">xml</span><span style="color: blue">:</span><span style="color: red">space</span><span style="color: blue">=&quot;preserve&quot;&gt;
    &lt;</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;</span><span style="color: #a31515">This
Text
Is
On
Separate
Lines
    </span><span style="color: blue">&lt;/</span><span style="color: #a31515">TextBlock.Text</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">TextBlock</span><span style="color: blue">&gt;
</span></pre>
<p>Finally, you have control over your whitespace back!&#160; The results are much more predictable, and you can now be explicit about what you want the text content to be.</p>
<p><strong>Please note:</strong> when you upgrade your application to Silverlight 4 (i.e. recompile for Silverlight 4), you’ll need to watch out for this change!</p>
<h3>ISupportInitialize</h3>
<p>If you were a fan of the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.isupportinitialize.aspx">ISupportInitialize</a> interface in the full framework, it’s finally made its way into Silverlight!&#160; The XAML parser in Silverlight will now call ISupportInitialize.BeginInit() and ISupportInitialize.EndInit() on your classes (if you implement the interface) before and after setting properties defined in XAML.&#160; This allows you to wait to do work until after all of your properties have been set, allowing you to handle properties that would otherwise be order-dependent and add some validation that combinations of properties are valid.</p>
<p>It’s a convenient and welcome functionality to have around, especially for those of us that like declarative programming and are happy to use XAML as our format for doing so!</p>
<h3>XmlnsDefinition attribute</h3>
<p>In Silverlight 3 and earlier, how many times did you find yourself in a situation like this?</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">UserControl </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Class</span><span style="color: blue">=&quot;SilverlightApplication1.MainPage&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">x</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">d</span><span style="color: blue">=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">mc</span><span style="color: blue">=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">controls</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">data</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">input</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">navigationMappings</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">navigation</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">ohmygoshtherearetoomanyofthese</span><span style="color: blue">=&quot;clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data&quot;
             </span><span style="color: red">mc</span><span style="color: blue">:</span><span style="color: red">Ignorable</span><span style="color: blue">=&quot;d&quot;
             </span><span style="color: red">d</span><span style="color: blue">:</span><span style="color: red">DesignHeight</span><span style="color: blue">=&quot;300&quot;
             </span><span style="color: red">d</span><span style="color: blue">:</span><span style="color: red">DesignWidth</span><span style="color: blue">=&quot;400&quot;&gt;...
</span></pre>
<p>And that’s just with some of the SDK assemblies!&#160; Declaring xmlns’s in your XAML was a chore, and it was extremely easy to end up with a huge mess of them, as they require one definition per namespace/assembly.&#160; Add in the toolkit controls and any custom libraries, and you’re easily looking at 20 lines of these definitions.&#160; This is more than just a problem of convenience – it makes XAML intellisense a big problem as well.&#160; Visual Studio starts its intellisense experience by waiting for an xmlns to be entered, such as “input:”.&#160; Within that namespace, it will filter your options to valid tags for your context within the XAML.</p>
<p>But, if you’re like me and like to break your libraries up into small pieces, this is not very helpful – I have to remember exactly which xmlns has which set of things that derives from the type I’m trying to create in XAML, and if I got it wrong, there’s not much there to help me.</p>
<p>Silverlight 4 adds support for the <a href="http://msdn.microsoft.com/en-us/library/system.windows.markup.xmlnsdefinitionattribute.aspx">XmlnsDefinitionAttribute</a> in custom assemblies, and the SDK has been updated to take advantage of this.&#160; Now, you can replace all of the code above with this:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">UserControl </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Class</span><span style="color: blue">=&quot;SilverlightApplication1.MainPage&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">x</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">d</span><span style="color: blue">=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">mc</span><span style="color: blue">=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
             </span><strong><em><u><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">sdk</span></u></em></strong><span style="color: blue"><strong><em><u>=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&quot;</u></em></strong>
             </span><span style="color: red">mc</span><span style="color: blue">:</span><span style="color: red">Ignorable</span><span style="color: blue">=&quot;d&quot;
             </span><span style="color: red">d</span><span style="color: blue">:</span><span style="color: red">DesignHeight</span><span style="color: blue">=&quot;300&quot;
             </span><span style="color: red">d</span><span style="color: blue">:</span><span style="color: red">DesignWidth</span><span style="color: blue">=&quot;400&quot;&gt;</span><span style="color: #a31515">...
</span></pre>
<p>Likewise, with the next update of the <a href="http://silverlight.codeplex.com">Silverlight Toolkit</a>, you will likely see a similar change, allowing you access to all referenced toolkit controls from one xmlns!&#160; Phew!&#160; What a relief!</p>
<p>With that in mind, I’ve also updated <a href="http://www.davidpoll.com/downloads-and-samples/#SLaB">SLaB</a> to use XmlnsDefinitionAttribute, which really helps clean up the use of those libraries with Silverlight 4.</p>
<p>This is one of my favorite features, if only because it makes working with XAML <em>so</em> much simpler in Visual Studio, and now it’s available for you and other control/XAML-centric developers to use in your libraries as well!</p>
<h3></h3>
<h3>Xmlns flexibility</h3>
<p>Silverlight 3 required that your default namespace always be the following:</p>
<pre class="code"><span style="color: red">xmlns</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
</span></pre>
<p>This was sometimes irritating when you wanted to use XAML as a serialization format for something other than UI, since you had to preface every element with some other xmlns.&#160; In Silverlight 4, you can now change the default xmlns namespace at will, which you can use to clean up your XAML and customize your serialization format as you wish.</p>
<h3></h3>
<h3>Custom IDictionary support</h3>
<p>In XAML, it’s supposed to be possible to add items to a dictionary just as you would a list by using the “x:Key” attribute.&#160; In prior versions of Silverlight, the only case where this was allowed was when used inside of a ResourceDictionary.</p>
<p>With Silverlight 4, anything that implements IDictionary can be used in XAML.&#160; To this end, I’ve provided a simple BindableDictionary in my SLaB libraries that you can now use like so (although you could just as easily use a Dictionary&lt;object, SomeType&gt; or a custom implementation of IDictionary):</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">BindableDictionary </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;ValueBag&quot;
                                </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">sys</span><span style="color: blue">=&quot;clr-namespace:System;assembly=mscorlib&quot;&gt;
    &lt;</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">Double </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;Value Between Zero and 100&quot;&gt;</span><span style="color: #a31515">37.9184273</span><span style="color: blue">&lt;/</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">Double</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">Boolean </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;A boolean&quot;&gt;</span><span style="color: #a31515">True</span><span style="color: blue">&lt;/</span><span style="color: #a31515">sys</span><span style="color: blue">:</span><span style="color: #a31515">Boolean</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SLaB</span><span style="color: blue">:</span><span style="color: #a31515">BindableDictionary</span><span style="color: blue">&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This is extremely useful for more complex XAML scenarios where you want a Dictionary rather than a List to be declared in XAML.&#160; One example I’ve been wanting to play with: an INavigationContentLoader (it’s an obsession – I’m sick, I know <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) that maps a protocol (e.g. http:// or pack://) to another INavigationContentLoader, allowing protocol-specific content loading within a single application.</p>
<h3></h3>
<h3>Non-DependencyProperty Attached Properties</h3>
<p>Although you’re probably used to seeing them as such, attached properties are actually completely separate from DependencyProperties (just as properties are distinct from DependencyProperties).&#160; In order to style or bind to them, they must be DependencyProperties.&#160; Otherwise, it’s just an API convention:</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Windows.Controls;

<span style="color: blue">namespace </span>SilverlightApplication1
{
    <span style="color: blue">public class </span><span style="color: #2b91af">ClassWithAttachedProperty
    </span>{
        <span style="color: blue">private static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Grid</span>, <span style="color: blue">int</span>&gt; values = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Grid</span>, <span style="color: blue">int</span>&gt;();
        <span style="color: blue">public static int </span>GetGridMetadata(<span style="color: #2b91af">Grid </span>g)
        {
            <span style="color: blue">return </span>values[g];
        }
        <span style="color: blue">public static void </span>SetGridMetadata(<span style="color: #2b91af">Grid </span>g, <span style="color: blue">int </span>someValue)
        {
            values[g] = someValue;
        }
    }
}</pre>
<p>Which can then be used in XAML like so:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">UserControl </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Class</span><span style="color: blue">=&quot;SilverlightApplication1.MainPage&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">x</span><span style="color: blue">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             </span><span style="color: red">xmlns</span><span style="color: blue">:</span><span style="color: red">my</span><span style="color: blue">=&quot;clr-namespace:SilverlightApplication1&quot;&gt;
    &lt;</span><span style="color: #a31515">Grid </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;LayoutRoot&quot; </span><strong><em><u><span style="color: red">my</span><span style="color: blue">:</span><span style="color: red">ClassWithAttachedProperty.GridMetadata</span></u></em></strong><span style="color: blue"><strong><em><u>=&quot;150&quot;</u></em></strong>&gt;
    &lt;/</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">UserControl</span><span style="color: blue">&gt;
</span></pre>
<p><strong>Note:</strong> I don’t recommend keeping a dictionary of Grid—&gt;int around <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .&#160; That’s a giant memory leak waiting to happen!&#160; It’s just convenient for demonstrative purposes.</p>
<h3>Better error messages</h3>
<p>One of the biggest complaints about XAML in Silverlight has been its poor reporting of errors when you’ve done something wrong.&#160; This should have significantly improved in Silverlight 4.&#160; In general, you’ll get more context and much clearer error messages at runtime when you make a mistake in XAML.&#160; In general, that feeling of “what the heck just happened?” should be lifting when it comes to working with XAML.&#160; It’s still not perfect, but things have improved significantly.</p>
<h3></h3>
<h3>Wow, that’s a lot of stuff!</h3>
<p>Yep, there’s been plenty of change with the parser, and the improvements should lead to a better experience when doing declarative development using XAML in Silverlight!&#160; Please let me know what you think!&#160; What’s your favorite feature or biggest peeve?</p>
<h3>What about SLaB?</h3>
<p>I’ve updated SLaB for the Silverlight 4 RC.&#160; There’s a bunch of new stuff in there that I hope to blog about soon.&#160; In the meantime, here’s a snippet from the changelog:</p>
<ul>
<li>For the latest version, please check out SLaB on my <a href="http://www.davidpoll.com/downloads-and-samples/#SLaB">Downloads and Samples</a> page. </li>
<li>The <a href="http://www.davidpoll.com/Download/SLaB/SLaBv0.4.zip">v0.4 download</a> of SLaB includes the following changes:
<ul>
<li>Added ZipUtilities so that the contents of a zip file can be discovered </li>
<li>Updated XapLoader to use ZipUtilities, allowing all TPEs to work rather than being limited to single-file TPEs where the file is named the same as the zip </li>
<li>Added Sitemap-based controls: BreadCrumbNavigator and TreeViewNavigator </li>
<li>Added ChangeLinq libraries for working with INotifyCollectionChanged collections and LINQ </li>
<li>Added ObservableDictionary and BindableDictionary, which raise INotifyCollectionChanged and INotifyPropertyChanged events as the dictionary changes, making it more usable with Binding </li>
<li>Added a basic MEFContentLoader </li>
<li>Updated to use XmlnsDefinitionAttribute wherever possible </li>
<li>Added XmlnsDefinition attributes to all libraries and updated ScratchApplication to use them </li>
<li>Other minor bugfixes </li>
</ul>
</li>
</ul>
<p>Enjoy, and let me know if you have any questions, thoughts, or ideas!</p>
<p><strong><em>Remember</em></strong>, SLaB is just a collection of the samples and experimental components I’ve been putting together so that they’re all in one place.&#160; I can’t make any guarantees about maintaining them, fixing bugs, not making breaking changes, etc., but you’re more than welcome to try them out, use them, and let them inspire your development (or show you what <em>not</em> to do if you really dislike something I’m doing!) <img class="wp-smiley" alt=":)" src="http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif" /> .</p>
<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Silverlight Toolkit November 2009: Activity Control &#8211;&gt; BusyIndicator (a.k.a. Update 3: Displaying background activity in a Silverlight RIA application)</title>
		<link>http://www.davidpoll.com/2009/11/19/silverlight-toolkit-november-2009-activity-control-busyindicator-a-k-a-update-3-displaying-background-activity-in-a-silverlight-ria-application/</link>
		<comments>http://www.davidpoll.com/2009/11/19/silverlight-toolkit-november-2009-activity-control-busyindicator-a-k-a-update-3-displaying-background-activity-in-a-silverlight-ria-application/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 06:14:45 +0000</pubDate>
		<dc:creator>david.poll</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Activity Control]]></category>
		<category><![CDATA[BusyIndicator]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[Silverlight 4 Beta]]></category>
		<category><![CDATA[Silverlight Toolkit]]></category>

		<guid isPermaLink="false">http://www.davidpoll.com/2009/11/19/silverlight-toolkit-november-2009-activity-control-busyindicator-a-k-a-update-3-displaying-background-activity-in-a-silverlight-ria-application/</guid>
		<description><![CDATA[Wow!&#160; What a week!&#160; PDC, the Silverlight 4 Beta, and now the November 2009 release of the Silverlight Toolkit!&#160; There’s been a ton of great news and exciting announcements, and now I can share with you that the Activity control, first blogged here, is now a part of the Silverlight Toolkit! Awesome!&#160; What does that [...]]]></description>
			<content:encoded><![CDATA[<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_start --><p>Wow!&#160; What a week!&#160; <a href="http://www.microsoftpdc.com" target="_blank">PDC</a>, the <a href="http://www.silverlight.net/getstarted/silverlight-4-beta" target="_blank">Silverlight 4 Beta</a>, and now the November 2009 release of the <a href="http://silverlight.codeplex.com/" target="_blank">Silverlight Toolkit</a>!&#160; There’s been a ton of great news and exciting announcements, and now I can share with you that the Activity control, <a href="http://www.davidpoll.com/tag/activity-control/" target="_blank">first blogged here</a>, is now a part of the Silverlight Toolkit!</p>
<h3>Awesome!&#160; What does that mean for the Activity control?</h3>
<p>During this transition, the Activity control underwent a few changes (thanks to my colleagues working on the Toolkit – <a href="http://blogs.msdn.com/delay/" target="_blank">David Anson</a> and <a href="http://www.jeff.wilcox.name" target="_blank">Jeff Wilcox</a>) to make it more generally palatable:</p>
<ul>
<li>The control has been renamed from “Activity” to “BusyIndicator” in order to avoid confusion with the concept of an Activity (sounds a lot like “Task”)</li>
<ul>
<li>Similarly, “IsActive” is now “IsBusy”, and the visual states have been renamed appropriately as well.</li>
</ul>
<li>AutoBind and related properties have been removed once and for all.&#160; They were a performance hog in the original versions, and they really only hit a very, very narrow scenario.&#160; Instead, bind to a property that represents your busyness.</li>
<li>MinDisplayTime has gone away.&#160; If you’re looking to change the minimum display time, you can re-template the control and add a duration to the transition from visible back to hidden.</li>
<li>OverlayBrush and OverlayOpacity have been replaced with a single OverlayStyle property where you can set the color/opacity of the overlay easily.</li>
<li>DisplayAfter now defaults to 0.1s instead of 0.05s</li>
</ul>
<p>Otherwise, the control is basically the same!&#160; Give it a shot!</p>
<p>With this transition, the BusyIndicator control truly becomes accessible to anyone who’s using the Silverlight Toolkit.&#160; You can <a href="http://silverlight.codeplex.com/WorkItem/List.aspx" target="_blank">report bugs through that project on codeplex</a>, and we’ll make sure to keep an eye on any feedback we get from you on the control.</p>
<h3>Great… So where can I find it?</h3>
<p>It’s simple!&#160; Just download the November 2009 Silverlight Toolkit for either Silverlight 3 or Silverlight 4.&#160; There are a few ways to get it:</p>
<ul>
<li><a href="http://silverlight.codeplex.com" target="_blank">Silverlight Toolkit Codeplex Page</a></li>
<li><a href="http://www.jeff.wilcox.name/2009/11/toolkit-webpi/" target="_blank">Web Platform Installer</a></li>
</ul>
<p>The BusyIndicatorControl can be found in the <strong>System.Windows.Controls</strong> namespace in the <strong>System.Windows.Controls.Toolkit.dll</strong> assembly in the toolkit.</p>
<h3></h3>
<h3>And… can I see it in action?</h3>
<p>Yep!&#160; David Anson created some awesome samples in the Toolkit Sample Browser for the BusyIndicator.&#160; I’m a big fan of his work! <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://silverlight.net/content/samples/sl3/toolkitcontrolsamples/run/default.html" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="BusyIndicator is now in the Silverlight Toolkit sample browser!" border="0" alt="BusyIndicator is now in the Silverlight Toolkit sample browser!" src="http://www.davidpoll.com/Images/Sil.Update3Displayingbackgroundactivityi_138BA/image.png" width="644" height="435" /></a> </p>
<p>I hope you all enjoy using the BusyIndicator control in your applications!&#160; Let me know what you think!</p>
<p>P.S. Thanks to Jeff Wilcox, who <a href="http://www.jeff.wilcox.name/2009/11/busy-indicator-control/" target="_blank">beat me to explaining this change</a> <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#160; I’ve been super-busy with PDC, so it was good to have someone start getting the word out early.&#160; Jeff gives a great overview of what the control is meant to do and has lots of resources regarding the Silverlight Toolkit.&#160; Check out his <a href="http://www.jeff.wilcox.name" target="_blank">blog</a>!</p>
<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://www.davidpoll.com/2009/11/19/silverlight-toolkit-november-2009-activity-control-busyindicator-a-k-a-update-3-displaying-background-activity-in-a-silverlight-ria-application/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Silverlight 3 Navigation: Adding transitions to the Frame control</title>
		<link>http://www.davidpoll.com/2009/07/19/silverlight-3-navigation-adding-transitions-to-the-frame-control/</link>
		<comments>http://www.davidpoll.com/2009/07/19/silverlight-3-navigation-adding-transitions-to-the-frame-control/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 22:03:26 +0000</pubDate>
		<dc:creator>david.poll</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Navigation]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[Silverlight Toolkit]]></category>

		<guid isPermaLink="false">http://www.davidpoll.com/2009/07/19/silverlight-3-navigation-adding-transitions-to-the-frame-control/</guid>
		<description><![CDATA[Continuing with my recent theme of enhancing the built-in support for Navigation in Silverlight 3, I thought I’d use this post to look briefly at enhancing user experience during navigation.&#160; On the surface, the Frame control is pretty unexciting from a UX perspective – its job is really just to display pages as a result [...]]]></description>
			<content:encoded><![CDATA[<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_start --><p>Continuing with my recent theme of enhancing the built-in support for Navigation in Silverlight 3, I thought I’d use this post to look briefly at enhancing user experience during navigation.&#160; On the surface, the Frame control is pretty unexciting from a UX perspective – its job is really just to display pages as a result of requests to navigate (either through the browser’s address bar, responding to HyperlinkButton clicks, or direct calls to Frame.Navigate()).&#160; It has barely any UI of its own, and can usually be thought of as an enhanced ContentControl (incidentally, it is one!).</p>
<p>Most of the time, all the Frame is doing is displaying a Page, and this (in my opinion at least) is an appropriate presentation – the Frame shouldn’t get in the way of displaying my Pages.&#160; Where I do want the Frame to intervene now and then is during page changes, allowing me to provide rich transitions when navigating from Page to Page.&#160; The Frame control provided in the Silverlight 3 SDK doesn’t do anything for us as far as transitions go out of the box, but with a little templating magic and the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a>’s TransitioningContentControl (which Jesse Liberty has a <a href="http://silverlight.net/blogs/jesseliberty/archive/2009/04/29/animated-visual-state-transitions-with-the-transitioning-content-control.aspx">great blog post on</a>), I think we can get the desired effect!</p>
<p>Let’s begin by taking a peek at the ControlTemplate for the Frame control:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">ControlTemplate </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;navigation:Frame&quot;&gt;
    &lt;</span><span style="color: #a31515">Border </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">}&quot;
            </span><span style="color: red">VerticalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">}&quot;
            </span><span style="color: red">Background</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Background</span><span style="color: blue">}&quot;
            </span><span style="color: red">BorderBrush</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">BorderBrush</span><span style="color: blue">}&quot;
            </span><span style="color: red">BorderThickness</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">BorderThickness</span><span style="color: blue">}&quot;&gt;
        &lt;</span><span style="color: #a31515">ContentPresenter </span><span style="color: red">Cursor</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Cursor</span><span style="color: blue">}&quot;
                          </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">}&quot;
                          </span><span style="color: red">Margin</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Padding</span><span style="color: blue">}&quot;
                          </span><span style="color: red">VerticalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">}&quot;
                          </span><span style="color: red">Content</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Content</span><span style="color: blue">}&quot;/&gt;
    &lt;/</span><span style="color: #a31515">Border</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">ControlTemplate</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>As you can see, there’s very little to it: a Border and a ContentPresenter – with all the appropriate TemplateBindings.</p>
<p>All we have to do to get transitions on the Frame control is replace that ContentPresenter with the TransitioningContentControl:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">ControlTemplate </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;TransitioningFrame&quot; </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;navigation:Frame&quot;&gt;
    &lt;</span><span style="color: #a31515">Border </span><span style="color: red">Background</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Background</span><span style="color: blue">}&quot;
            </span><span style="color: red">BorderBrush</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">BorderBrush</span><span style="color: blue">}&quot;
            </span><span style="color: red">BorderThickness</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">BorderThickness</span><span style="color: blue">}&quot;
            </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">}&quot;
            </span><span style="color: red">VerticalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">}&quot;&gt;
        &lt;</span><span style="color: #a31515">toolkit</span><span style="color: blue">:</span><span style="color: #a31515">TransitioningContentControl </span><span style="color: red">Content</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Content</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">Cursor</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Cursor</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">Margin</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">Padding</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">HorizontalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">VerticalAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">HorizontalContentAlignment</span><span style="color: blue">}&quot;
                                             </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">TemplateBinding </span><span style="color: red">VerticalContentAlignment</span><span style="color: blue">}&quot;
                                             </span><span style="color: red"><strong><u><em>Transition</em></u></strong></span><span style="color: blue"><strong><u><em>=&quot;&lt;Transition Name&gt;&quot;</em></u></strong> /&gt;
    &lt;/</span><span style="color: #a31515">Border</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">ControlTemplate</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The “toolkit:” xml namespace uses the following definition (since you can find the TransitioningContentControl in the System.Windows.Controls namespace and the System.Windows.Controls.Layout.Toolkit assembly): xmlns:toolkit=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit&quot;</p>
<p>To change the type of transition the Frame will use, just change the value of the Transition property on the TransitioningContentControl.</p>
<p>There’s not much more to it!&#160; Short, sweet, and to the point <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Check out the live sample and source if you’re curious:</p>
<ul>
<li><a href="http://www.davidpoll.com/Samples/TransitioningNavigation/TestPage.html">Sample</a> </li>
<li><a href="http://www.davidpoll.com/Download/TransitioningNavigation.zip">Sample Source</a> </li>
</ul>
<p>&#160;</p>
<p><strong><font size="4">Caveats and Questions</font></strong></p>
<p>I know you’d be disappointed if I really left it at that, so here’s the one caveat I’ve observed with this technique:</p>
<p>My <a href="http://www.davidpoll.com/?p=92">dynamically-loaded libraries with navigation workaround</a> will cause a few problems with this technique, since it sets the Frame’s Content property twice (once to load the DynamicPageShim, the other to load the DynamicPage itself).&#160; The result is that you will see the Frame transition to a blank page and then to the DynamicPage with your content.&#160; So here’s the question: how important is this transitioning behavior to you?&#160; There are two options for how I proceed here, neither of which are truly ideal:</p>
<ol>
<li>Leave DynamicPage and DynamicPageShim as-is and live with the slight wonkiness during transitions. </li>
<li>Make DynamicPageShim avoid changing the content of the Frame twice (making transitions work just fine <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).&#160; This will mean that you won’t be able to use the DynamicPage<strong><em>exactly</em></strong> as you would the standard Page (e.g. binding to &quot;Frame.Content.MyCustomPropertyOnMyPage” won’t be possible… instead you’d have to bind to “Frame.Content.<strong><em>Content</em></strong>.MyCustomPropertyOnMyPage”). </li>
</ol>
<p>Neither of these options significantly hampers functionality – just impacts either user experience or developer experience, but I’d love to hear your thoughts!&#160; Do you prefer option 1 or option 2?</p>
<p>In the meantime, I’m working on some fun little utilities that will make dynamically loading assemblies with navigation much simpler and more declarative, and am looking forward to blogging it when it’s ready!</p>
<p>&#160;</p>
<p><strong><font size="4">Update: Credit where credit is due</font></strong></p>
<p>About 10 minutes after I posted this, I came across someone (<a href="http://firstfloorsoftware.com/blog/">Koen Zwikstra</a>) who posted almost the exact same thing before me, and I want to make sure he gets fair mention for getting there before me <img src='http://www.davidpoll.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You can find his post here: <a title="http://firstfloorsoftware.com/blog/animated-page-navigation-in-sl3/" href="http://firstfloorsoftware.com/blog/animated-page-navigation-in-sl3/">http://firstfloorsoftware.com/blog/animated-page-navigation-in-sl3/</a></p>
<p>Sorry for duplicating!&#160; Great stuff!</p>
<!-- Advanced AdSense by Jim Gaudet --><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://www.davidpoll.com/2009/07/19/silverlight-3-navigation-adding-transitions-to-the-frame-control/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

