<?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>JAPF &#187; transition</title>
	<atom:link href="http://www.japf.fr/tag/transition/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adding transitions to a MVVM based dialog</title>
		<link>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/</link>
		<comments>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 18:25:09 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[transition]]></category>
		<category><![CDATA[user experience]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=182</guid>
		<description><![CDATA[In my last blog post about MVVM, I showed how it is natural to build a common WPF dialog using DataBinding and Templates with the Model-View-ViewModel methodology. Because I&#8217;m having a lot of feedback on posts I wrote about how transitions can be done in WPF, I decided to reuse my previous demo application and [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.japf.fr/?p=154">last blog post</a> about MVVM, I showed how it is natural to build a common WPF dialog using DataBinding and Templates with the Model-View-ViewModel methodology. Because I&#8217;m having a lot of feedback on posts I wrote about how transitions can be done in WPF, I decided to reuse my previous demo application and add transitions when switching from one element to another. Here is the result of the demo application:</p>
<p><img src="http://www.japf.fr/wp-content/plugins/flash-video-player/default_video_player.gif" /></p>
<p>I didn&#8217;t wanted to implement all the transitions, instead I decided to use the famous <a href="http://www.codeplex.com/fluidkit">FluidKit</a> library and to wrap its transition control into a reusable and &#8220;MVVM compliant&#8221; control. I thank <a href="http://blog.pixelingene.com/">Pavan Podila</a> for giving me feedback while designing this control.</p>
<p>Basically, based on Pavan suggestions I created an INavigationAware interface and subclass transitions I wanted to reuse from FluidKit. The INavigationAware interface allows me to specify that the transition supports going forward and going backward (regarding the previous and current selection of the user in the menu).</p>
<p>The control itself (that I call a NavigationPresenter) is very simple, I just use 2 ContentPresenter that I switch when the Content property changes using the TransitionPresenter control from the FluidKit library. The NavigationPresenter works with 3 dependency properties:</p>
<ul>
<li>GoForward (bool): to specifiy the way of the transition</li>
<li>Transition: to specify the transition to use</li>
<li>Content: to specify the content of the control</li>
</ul>
<p>Here is the XAML for using the NavigationPresenter</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p182code2'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1822"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p182code2"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- The ContentProperty is not bound directly to the SelectedItem of the ListBox because the</span>
<span style="color: #808080; font-style: italic;">     GoForward property must be updated BEFORE the content changes. The CurrentContent property</span>
<span style="color: #808080; font-style: italic;">     is defined in the ViewModel class and updated everytime the selection of the ListBox changes,</span>
<span style="color: #808080; font-style: italic;">     after setting up the GoForward value. --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;NavigationTransition:NavigationPresenter</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{Binding CurrentContent}&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">Transition</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=transitionComboBox, Path=SelectedItem.Tag}&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">GoForward</span>=<span style="color: #ff0000;">&quot;{Binding DataContext.GoForward, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type AnimatedContentPresenter:MainDialog}}}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>            
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/NavigationTransition:NavigationPresenter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The sample application comes with 3 animations but more can be used from the FluidKit library. It&#8217;s also possible to create your own transition (inherit from Transition base class). You can download the sample application <a href="http://www.japf.fr/download/AnimatedContentPresenter.zip">here</a>. Hope you&#8217;ll like it !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>TransitionPresenter control from FluidKit</title>
		<link>http://www.japf.fr/2008/12/transitionpresenter-control-from-fluidkit/</link>
		<comments>http://www.japf.fr/2008/12/transitionpresenter-control-from-fluidkit/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 12:09:51 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Pages]]></category>
		<category><![CDATA[slide]]></category>
		<category><![CDATA[transition]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=80</guid>
		<description><![CDATA[By looking at Worpress statistics for my blog, I realized that many people were coming here by searching keywords like &#8220;animation&#8221;, &#8220;transition&#8221;, etc. A couple of month ago, I wrote an article about a control that I created: the ContentSlider control. The article described a control that could be used to create an animation when [...]]]></description>
			<content:encoded><![CDATA[<p>By looking at Worpress statistics for my blog, I realized that many people were coming here by searching keywords like &#8220;animation&#8221;, &#8220;transition&#8221;, etc. A couple of month ago, I wrote <a href="http://www.japf.fr/?p=18">an article</a> about a control that I created: the ContentSlider control. The article described a control that could be used to create an animation when switching from one page to another, but it was very basic.</p>
<p>If you&#8217;re looking for a more complete solution, I recommend you to take a look at the <a href="http://www.codeplex.com/fluidkit">FluidKit library</a> created by <a href="http://blog.pixelingene.com">Paven Podila</a>. The FluidKit library contains very powerful control, the most known is probably the ElementFlow control that is similar to Apple&#8217;s famous CoverFlow functionality:</p>
<p style="text-align: center;"><a href="http://www.japf.fr/wp-content/uploads/2008/12/fluidkit-elementflow.jpg" rel="lightbox[80]"><img class="size-medium wp-image-81 aligncenter" title="fluidkit-elementflow" src="http://www.japf.fr/wp-content/uploads/2008/12/fluidkit-elementflow-300x161.jpg" alt="" width="374" height="201" /></a></p>
<p>FluidKit also contains a TransitionPresenter control that is an extended and more powerful version of my ContentSlider control. The TransitionPresenter control comes with 4 transitions that you can easily use in your application (Cube, Flip, Slide, Genie):</p>
<p style="text-align: center;"><a href="http://www.japf.fr/wp-content/uploads/2008/12/fluidkit-transition-presenter.jpg" rel="lightbox[80]"><img class="size-medium wp-image-82 aligncenter" title="fluidkit-transition-presenter" src="http://www.japf.fr/wp-content/uploads/2008/12/fluidkit-transition-presenter-300x198.jpg" alt="" width="300" height="198" /></a></p>
<p>Pavan really did a great job with the FluidKit library. Go ahead a grab the source to play with it and learn very cool stuff about WPF !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/12/transitionpresenter-control-from-fluidkit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introducing the ContentSlider control</title>
		<link>http://www.japf.fr/2008/07/introducing-the-contentslider-control/</link>
		<comments>http://www.japf.fr/2008/07/introducing-the-contentslider-control/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 07:05:57 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[transition]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=18</guid>
		<description><![CDATA[In my very first article, I wrote an article about an implementation of a solution allowing to use transition (using a sliding effect) between views in a WPF application. As a reminder, here when I talk about &#8220;views&#8221; I mean a screen of the application. One of this screen could be used as a login [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.japf.fr/?p=8">my very first article</a>, I wrote an article about an implementation of a solution allowing to use transition (using a sliding effect) between views in a WPF application.</p>
<p>As a reminder, here when I talk about &#8220;views&#8221; I mean a screen of the application. One of this screen could be used as a login screen, the other one to setup parameters, etc.</p>
<p>In this article, I introduce a CustomControl that I build in order to re-use the implementation I described in an easy way. Basically, using this control, you can write something like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p18code4'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p184"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p18code4"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;control:ContentSlider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- here is the content --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/control:ContentSlider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>All the specific stuff related to the transition (the animation, the use of a VisualBrush&#8230;) is handled INSIDE the control so that it is completely transparent to use.</p>
<p>From C# code point of view, you&#8217;ll need to use 3 functions:</p>
<ul>
<li>PrepareSlide()</li>
<li>SlideToLeft()</li>
<li>SlideToRight()</li>
</ul>
<p>The first one (PrepareSlide) must be called right before changing the content of the view. It will create a &#8220;snapshot&#8221; of the view so that it will become possible to animate it.</p>
<p>The next two one should be called to slide the content to the left (or to the right).</p>
<p>I hope you&#8217;ll enjoy it. <a href="http://www.japf.fr/download/japf_fr_contentslider.zip">Here is a solution</a> containing the control with example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/07/introducing-the-contentslider-control/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using animated navigation pages in a WPF application</title>
		<link>http://www.japf.fr/2008/07/8/</link>
		<comments>http://www.japf.fr/2008/07/8/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 11:00:00 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[imagebrush]]></category>
		<category><![CDATA[transition]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=8</guid>
		<description><![CDATA[Article updated ! You can now download the example as a Visual Studio 2008 solution. Article updated ! For a more complete solution, please take a look at this post Introduction Before going into fun topics, let me give some information about myself. I started to play with WPF about 1 year ago. Since the [...]]]></description>
			<content:encoded><![CDATA[<p>Article updated ! You can now <a href="http://www.japf.fr/download/japf_fr_slide_animation.zip">download the example</a> as a Visual Studio 2008 solution.<br />
Article updated ! For a more complete solution, please take a look at <a href="http://www.japf.fr/?p=182">this post</a></p>
<h3>Introduction</h3>
<p>Before going into fun topics, let me give some information about myself. I started to play with WPF about 1 year ago. Since the beginning, I fell in love with this new technology and the more I work with it, the more I like it. Today, I consider I’m still a newbie in the WPF world and this is my first article, so please forgive me if I do some mistakes. Please also forgive my English as this is not my natural language (I’m French).</p>
<p>In this article, I describe a technique that I’ve been using in order to use navigation pages in a WPF application. In the next section, I’m going to give more details about what I mean by “navigation pages”.</p>
<h3>Requirements</h3>
<p>Many applications are made up several screens. Here when I use the word “screen” I mean a view in the application. For example, you might have a view as a welcome screen and several other views.</p>
<p>In this case, you would like the current screen to display the current view, but you also need a technique to switch from one view to another. If you also want to improve the user experience, you might want to have some kind of animation when the user switches from one view to another.</p>
<p>A possible animation is to use a “slide effect”:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2008/07/slide1.png" rel="lightbox[8]"><img style="vertical-align: text-top;" title="slide1" src="http://www.japf.fr/wp-content/uploads/2008/07/slide1.png" alt="Using animation when changing current views" width="500" height="342" /></a></p>
<p>I’m going to describe 2 techniques that could be used to achieve this behaviour. I will also show you why the last one is better than the first one.</p>
<p><span id="more-8"></span></p>
<h3>A possible implementation</h3>
<p>By looking at the previous diagram, we can imagine a first implementation. Why not playing with the Margin property to slide out the view that we want to hide? Let’s try to implement this first solution…</p>
<p>I use a grid with a single cell to overlay the views. Please also note that in this very simple example, I used a TextBlock inside each view. We can of course imagine that we would put a custom UserControl containing the real view (for example the first one with a welcome image, the next one a basic form…).</p>
<p><em>Here is the main window:</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code14'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p814"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p8code14"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;FirstTry.Window1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Window1&quot;</span> <span style="color: #000066;">SizeToContent</span>=<span style="color: #ff0000;">&quot;WidthAndHeight&quot;</span> <span style="color: #000066;">ResizeMode</span>=<span style="color: #ff0000;">&quot;NoResize&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;previousButton&quot;</span> <span style="color: #000066;">Click</span>=<span style="color: #ff0000;">&quot;previousButton_Click&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Previous<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;nextButton&quot;</span> <span style="color: #000066;">Click</span>=<span style="color: #ff0000;">&quot;nextButton_Click&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Next<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;view3&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        View 3
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;view2&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;AntiqueWhite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        View 2
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;view1&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;AliceBlue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        View 1
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>       
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In the code behind, I update the Margin property of the Border elements regarding the one I’ve to display in the UI.</p>
<p><em>Code behind:</em></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code15'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p815"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code" id="p8code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Controls</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> FirstTry
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// </span>
    <span style="color: #008080; font-style: italic;">/// Interaction logic for Window1.xaml</span>
    <span style="color: #008080; font-style: italic;">/// </span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Window1 <span style="color: #008000;">:</span> Window
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> currentView <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Window1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> slide<span style="color: #008000;">&#40;</span>Border oldVisual, Border newVisual<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            oldVisual<span style="color: #008000;">.</span><span style="color: #0000FF;">Margin</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thickness<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">300</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            newVisual<span style="color: #008000;">.</span><span style="color: #0000FF;">Margin</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thickness<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> previousButton_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>currentView<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">:</span>
                    slide<span style="color: #008000;">&#40;</span>view2, view1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    currentView<span style="color: #008000;">--;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">:</span>
                    slide<span style="color: #008000;">&#40;</span>view3, view2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    currentView<span style="color: #008000;">--;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> nextButton_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>currentView<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">:</span>
                    slide<span style="color: #008000;">&#40;</span>view1, view2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    currentView<span style="color: #008000;">++;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">:</span>
                    slide<span style="color: #008000;">&#40;</span>view2, view3<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    currentView<span style="color: #008000;">++;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In my opinion, the problems with this first implementation are the following:</p>
<ul>
<li>no flexibility at all</li>
<li>memory consumption : all pages are loaded into memory even those which are not visible</li>
</ul>
<p>I didn’t add animation to this first example but I let you imagine that we could achieve the “slide effect” by animating the margin property (or using a TranslateTransform).</p>
<h3>Using an ItemsControl and an ImageBrush</h3>
<p>A problem with the previous implementation is that all views are loaded into memory, even those that never got displayed.</p>
<p>How could we improve that? The solution I present here uses an ItemsControl as view’s host control. I also use a ListBox to display available views through a binding to XML data (in order to improve flexibility).</p>
<p>The XML data:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code16'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p816"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p8code16"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;XmlDataProvider</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;views&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;x:XData<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Views</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View</span> <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;View1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Page</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;view1.xaml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/View<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View</span> <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;View2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Page</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;view2.xaml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/View<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View</span> <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;View3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Page</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;view3.xaml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/View<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Views<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/x:XData<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/XmlDataProvider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Here, I basically describe the available views in my application. Using this XML formatting, it becomes very straightforward to add or remove a view. I’m going to use this XMLDataProvider as a source data in order to bind my other controls to it.</p>
<p>The ListBox:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code17'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p817"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p8code17"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;viewList&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">SelectedIndex</span>=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Source={StaticResource views}, XPath=Views/View}&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">DisplayMemberPath</span>=<span style="color: #ff0000;">&quot;@Title&quot;</span>                    </span>
<span style="color: #009900;">    <span style="color: #000066;">SelectionChanged</span>=<span style="color: #ff0000;">&quot;viewList_SelectionChanged&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Here the ListBox is used to display the available views of in the application. It just setup is ItemsSource property in order to bind it to the XML data. I also specify an ItemsPanel in order to change the default vertical StackPanel to a horizontal one.</p>
<p>The ItemsControl:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code18'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p818"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p8code18"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;viewer&quot;</span> <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{Binding Path=SelectedItem, ElementName=viewList}&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding XPath=Page}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Frame</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;frame&quot;</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding XPath=@Source}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TranslateTransform</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The ItemsControl is used to contain the current view. I setup an ItemTemplate to specify that each item in the control should be rendered using a frame. This frame has its Source property bound to the XML source data. I also add a TranslateTransform in the RenderTransform collection in order to be able to animate this ItemsControl.</p>
<p>The problem with this news solution is that we have only one item (the ItemsControl), that seems to be hard to build a transition only with that… The solution I found uses an ImageBrush.</p>
<p>An ImageBrush allows painting a control with an image. By using an ImageBrush we can paint a Rectangle with the content of the current view. Then, when we change the current view, we can animate both Rectangle and the new current view:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2008/07/slide2.png" rel="lightbox[8]"><img style="vertical-align: text-top;" title="slide1" src="http://www.japf.fr/wp-content/uploads/2008/07/slide2.png" alt="Using animation when changing current views" width="300" height="282" /></a></p>
<p>In order to create this ImageBrush, I use a function that converts any FrameworkElement into a bitmap:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code19'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p819"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p8code19"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> RenderTargetBitmap RenderBitmap<span style="color: #008000;">&#40;</span>FrameworkElement element<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> topLeft <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> topRight <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> width <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> height <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> dpiX <span style="color: #008000;">=</span> <span style="color: #FF0000;">96</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// this is the magic number</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> dpiY <span style="color: #008000;">=</span> <span style="color: #FF0000;">96</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// this is the magic number</span>
&nbsp;
    PixelFormat pixelFormat <span style="color: #008000;">=</span> PixelFormats<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">;</span>
    VisualBrush elementBrush <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> VisualBrush<span style="color: #008000;">&#40;</span>element<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    DrawingVisual visual <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DrawingVisual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    DrawingContext dc <span style="color: #008000;">=</span> visual<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderOpen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    dc<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawRectangle</span><span style="color: #008000;">&#40;</span>elementBrush, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #008000;">new</span> Rect<span style="color: #008000;">&#40;</span>topLeft, topRight, width, height<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dc<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    RenderTargetBitmap bitmap <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RenderTargetBitmap<span style="color: #008000;">&#40;</span>width, height, dpiX, dpiY, pixelFormat<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    bitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Render</span><span style="color: #008000;">&#40;</span>visual<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> bitmap<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>By using this procedure, it becomes possible to paint the content of a rectangle with a ‘snapshot’ of the current view (just before it changes). I do this in the SelectionChanged event handler:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code20'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p820"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p8code20"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> viewList_SelectionChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, SelectionChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    XmlElement root <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>XmlElement<span style="color: #008000;">&#41;</span>viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span><span style="color: #008000;">;</span>
    XmlNodeList xnl <span style="color: #008000;">=</span> root<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectNodes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Page&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        RenderTargetBitmap rtb <span style="color: #008000;">=</span> RenderBitmap<span style="color: #008000;">&#40;</span>viewer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        rectanglevisual<span style="color: #008000;">.</span><span style="color: #0000FF;">Fill</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ImageBrush<span style="color: #008000;">&#40;</span>BitmapFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>rtb<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> xnl<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>oldSelectedIndex <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> viewList<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedIndex</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
   viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginStoryboard</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Storyboard<span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Resources</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;slideLeftToRight&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
   viewer<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginStoryboard</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Storyboard<span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Resources</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;slideRightToLeft&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    oldSelectedIndex <span style="color: #008000;">=</span> viewList<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedIndex</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I also start the animations in this EventHandler. One animation is responsible for sliding out the bordervisual and sliding in the new viewer.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code21'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p821"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p8code21"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Storyboard</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;slideLeftToRight&quot;</span>  </span>
<span style="color: #009900;">            <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;RenderTransform.(TranslateTransform.X)&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">AccelerationRatio</span>=<span style="color: #ff0000;">&quot;.4&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">DecelerationRatio</span>=<span style="color: #ff0000;">&quot;.4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DoubleAnimation</span> <span style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;viewer&quot;</span> <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;0:0:0.6&quot;</span> <span style="color: #000066;">From</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DoubleAnimation</span> <span style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;bordervisual&quot;</span> <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;0:0:0.6&quot;</span> <span style="color: #000066;">From</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;-300&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Storyboard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Storyboard</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;slideRightToLeft&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;RenderTransform.(TranslateTransform.X)&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">AccelerationRatio</span>=<span style="color: #ff0000;">&quot;.4&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">DecelerationRatio</span>=<span style="color: #ff0000;">&quot;.4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DoubleAnimation</span> <span style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;viewer&quot;</span> <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;0:0:0.6&quot;</span> <span style="color: #000066;">From</span>=<span style="color: #ff0000;">&quot;-300&quot;</span> <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DoubleAnimation</span> <span style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;bordervisual&quot;</span> <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;0:0:0.6&quot;</span> <span style="color: #000066;">From</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Storyboard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In order to make the animation works properly, I need to have both items (the current view and the snapshot of the previous view) in the same control, that’s why I’m using a Grid:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p8code22'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p822"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p8code22"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;bordervisual&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;rectanglevisual&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TranslateTransform</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;viewer&quot;</span> <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{Binding Path=SelectedItem, ElementName=viewList}&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding XPath=Page}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Frame</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;frame&quot;</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding XPath=@Source}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TranslateTransform</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>In this article, I showed a technique that can be used to build navigation based application with transition effect. Of course there are a lot of things that could be improved, I didn’t want to write something complete but I wanted to demonstrate the general idea.</p>
<p>This is my very first article about WPF, so please don’t be too rude with me! I hope you like it. Please don’t hesitate to give your feedback to improve this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/07/8/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
