SilverlightShow: Silverlight Community http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples SilverlightShow.net http://www.rssboard.org/rss-specification Argotic Syndication Framework 2008.0.2.0, http://www.codeplex.com/Argotic en-US estoychev@completit.com (Emil Stoychev) Using the Live SDK in Windows 8 XAML/C# Metro Applications <table width="20"> <tbody> <tr> <td> <div class="fb-like" data-show-faces="true" data-send="false" data-href="http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx" data-font="segoe ui" data-layout="button_count"></div> </td> <td><a href="https://twitter.com/share" class="twitter-share-button" data-via="silverlightshow" data-counturl="http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx" data-count="horizontal" data-text="Reading @mbcrump's article 'Using the Live SDK in #Windows8 XAML/C# Metro Applications' #win8" data-url="http://slshow.net/zsh5cA">Tweet</a></td> <td><g:plusone size="medium" href="http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx"></g:plusone> </td> <td> </td> </tr> </tbody> </table> <h3>Introduction</h3> <p>The Microsoft’s Live SDK (or commonly referred to as Live Connect), provides a set of controls and APIs that enables applications to integrate single sign-on (SSO) functionality using Windows Live ID. You can also use it to access data in SkyDrive, Hotmail, and Windows Live Messenger.</p> <div style="border:1px solid #dddddd;width: 200px; padding-top: 5px; padding-bottom: 5px; padding-left: 10px; margin-top: 5px; margin-right: 5px; margin-left: 10px; float: right; background-color: #f3f3f3; border-image: initial;"> <h3>Don't miss...</h3> <ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px; list-style-type: circle;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/video/Recording-of-Webinar-Introduction-to-XAML-Development-on-Windows-8-by-Gill-Cleeren.aspx">Webinar recording: Introduction to XAML Development on Windows 8</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/news/Free-SilverlightShow-Webinar-Metro-and-WinRT-for-the-Silverlight-WPF-Developer-.aspx">Webinar on Feb 2nd: Metro and WinRT for the Silverlight/WPF Developer</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/win8_odata.aspx">Ebook: Windows 8 XAML Metro Apps with OData</a> </li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/win8_odata.aspx" target="_self"><img style="width: 107px; height: 150px;" alt="Ebook: Windows 8 XAML Metro Apps with OData" src="http://www.silverlightshow.net/Storage/Ebooks/win8_odata.png" /></a></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> The library supports multiple platforms, including Windows Phone 7 Mango and Windows 8 Metro Style applications using either C#, Visual Basic or JavaScript.  In this article, we will build our first metro style application using the Live SDK and XAML/C#. <h3>Download the Bits</h3> <p>Let’s begin by downloading the following items:</p> <ul> <li>You will need to download the <a href="http://msdn.microsoft.com/en-us/windows/">Windows 8 Developer Preview</a> to follow along with this article. This will install Windows 8 along with the new Visual Studio 11 which is required to build Metro applications. </li> <li>Download the <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=28195">Live SDK v5.0</a> or higher - The Live SDK provides a set of controls and APIs that enable applications to integrate single sign-on (SSO) using Windows Live ID and to access data in SkyDrive, Hotmail, and Windows Live Messenger. </li> <li>Optional, but highly recommended - The <a href="http://code.msdn.microsoft.com/windowsapps/">Metro Style App Samples</a> which provides various sample metro applications for future reference. It is licensed for public use, so feel free to use it in your own applications. </li> <li>Optional, but recommended - Read the <a href="http://download.microsoft.com/download/1/E/4/1E455D53-C382-4A39-BA73-55413F183333/Windows_Developer_Preview-Windows8_guide.pdf">Windows 8 Developer Preview guide</a> to start learning about the features/functionality coming in Windows 8. </li> </ul> <h3> <br /> Signing into Windows 8 Using your Windows Live ID</h3> <p>One of the most important things to learn before building your first application that uses the Live SDK is the various ways to sign into your Windows 8 machine. <br /> <br /> You currently have two ways to sign into your Windows 8 machine and each way determines the prompts the users will receive when launching a application that uses the Live SDK. </p> <ol> <li>A Local Account – which stores all of the data regarding your profile on the local machine. <ul> <li>If you user is using this method, then they will get the prompt to sign in and to allow access. </li> </ul> </li> <li>A Windows Live ID – retrieves profile information directly from your Live ID and enables features such as roaming and easy sign-on to Microsoft services. <ul> <li>If you user is using this method, then they will only get the prompt to allow access.  </li> </ul> </li> </ol> <p>In this tutorial, we will be signing into <strong>Windows 8 using our Live ID</strong>. </p> <p>If you have already setup a “Local Account” then you can switch very easily by going to Control Panel –> Users then clicking on “Switch to a Windows Live ID” as shown below. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/__1_4.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="1" alt="1" src="http://www.silverlightshow.net/Storage/Users/mbcrump/__1_thumb_1.png" width="574" height="108" /></a></p> <p>You will then be prompted to enter your current password of the local account and log in using your Live ID username/password. If you don’t have one then you may sign up for one through this wizard. </p> <p>Immediately following you should receive an email similar to the one below: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/___________2_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="2" alt="2" src="http://www.silverlightshow.net/Storage/Users/mbcrump/___________2_thumb.png" width="569" height="301" /></a></p> <p>If you click on the link to confirm your identity then a web browser will pop open and you will be greeted with “Manage Security Info”. This screen will list mobile devices, email address and trusted PC linked to your account. As we can see below that I have three Windows 8 machines linked to my account. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/__________3_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="3" alt="3" src="http://www.silverlightshow.net/Storage/Users/mbcrump/__________3_thumb.png" width="331" height="106" /></a></p> <p>You can also add a new trusted PC manually with the “Add New” link. To use this feature, you must access your account using Internet Explorer and have Windows Live Essentials installed.</p> <h3>Let’s Get Started</h3> <p>Now that we have the necessary tools to begin building a Metro Application using C#/XAML and we we have signed into Windows 8 using our Live ID, let’s begin. </p> <p>In this sample application, we are going to retrieve the following information from the users profile: </p> <ul> <li>Users First and Last Name </li> <li>What gender they are and what region they live in </li> <li>URL to their Live Profile </li> </ul> <p>As well as add a contact to their address book. </p> <p>Launch Visual Studio 11 Developer Preview and select <strong>Visual C#</strong> -> <strong>Windows Metro style</strong> –> <strong>Application</strong>. Give the project the name <strong>MyFirstLiveConnectApp</strong> and hit OK. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/_________4_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="4" alt="4" src="http://www.silverlightshow.net/Storage/Users/mbcrump/_________4_thumb.png" width="663" height="419" /></a></p> <h3>Adding the Live SDK References</h3> <p>In case you haven’t already, please download and install the <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=28195">Live SDK v5.0</a> before proceeding.  </p> <p>Once that is complete, we can add the references to our project by Right Clicking on References inside of Visual Studio 11 and selecting “Add Reference”. Next, you will need to select Windows –> Extension SDKs –> Live SDK then Add. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/_5_4.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="5" alt="5" src="http://www.silverlightshow.net/Storage/Users/mbcrump/_5_thumb_1.png" width="629" height="353" /></a></p> <h3>Registering the Application with Windows Live</h3> <p>Before we begin building this app, you need to register it with Windows Live.  You can register your app by going to the <a href="https://manage.dev.live.com/build">application management site</a> and following the instructions on the site.</p> <p>The only item that you want to pay special attention to is the <strong>Package Name</strong> in Step 3 as you will copy and paste that into your <strong>Application’s Package.appxmanifest</strong> file. (A walk-through is included in that site).</p> <p>If you don’t register your application and try to use Live services, then your user will be presented with the following error message. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/6_6.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="6" alt="6" src="http://www.silverlightshow.net/Storage/Users/mbcrump/6_thumb_2.png" width="469" height="114" /></a></p> <p>Once this process is complete you can view your registered applications <a href="https://manage.dev.live.com/Applications/Index">here</a>. This page will contain all the API settings needed to use the Live SDK in your projects. </p> <h3>Understanding what is under the hood…</h3> <p>When we select the C#/XAML Application template, Visual Studio 11 provides us with basic application structure. Before we begin building our own, it makes sense to spend a few minutes examining the XAML pages included in the existing structure.  </p> <ul> <li><strong>References – </strong>It is empty by default because Microsoft decided that all the WinRT assemblies are always referenced by default and they have no reason to display them. Any of your own referenced assemblies or third party references will be shown as in this case our Windows Live SDK is shown. </li> <li><strong>App.xaml</strong> is used by Metro applications (amongst other XAML technologies) to declare shared resources like brushes, various style objects etc. The code behind file of App.xaml is used to handle global application level events like OnLaunched and OnActivated. </li> <li><strong>MainPage.xaml</strong> contains the markup of what our user interface will look like once the application is executed. The default Metro MainPage.xaml file consists of a UserControl and a Grid. </li> <li><strong>Package.appxmanifest</strong> – Is basically an XML file, but if you double click it then you will notice four tab panels inside it (named: Application UI, Capabilities, Declarations and Packaging) </li> </ul> <h3> <br /> Back to Building a Simple User Interface</h3> <p>Here is a mock-up screen of what our app will look like: </p> <h3><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/_____8_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="8" alt="8" src="http://www.silverlightshow.net/Storage/Users/mbcrump/______8_thumb.png" width="556" height="271" /></a></h3> <p>As you can see we have 2 buttons: </p> <ol> <li>Windows Live Sign-In – Provided by the Live SDK and is used to log the user into the app to retrieve Live Services information. </li> <li>Create Contact – Just a regular button that will be used to create a contact in the users address book. </li> </ol> <p>On the left hand side you can see information retrieved by the Live SDK including first and last name, gender, location and live profile URL. </p> <p>Double click on your MainPage.xaml and replace the existing Grid with the code snippet provided below.</p> <div style="border:1px solid silver;border-image: initial; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="#FF0C0C0C"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="251*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="1116*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="2"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">live:SignInButton</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="btnLogIn"</span> <span style="color: #ff0000;">Scopes</span><span style="color: #0000ff;">="wl.signin wl.basic wl.contacts_create"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="btnCreateContact"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Create Contact"</span> <span style="color: #ff0000;">Click</span><span style="color: #0000ff;">="btnCreateContact_Click"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="tbName"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="600"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="150"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="32"</span> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="tbGender"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="600"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="150"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="32"</span> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="tbLiveProfile"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="600"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="150"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="32"</span> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="tbError"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="Error Message"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="600"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="150"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="32"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>You will also want to make sure that your XML Namespaces for the Live SDK have been added to the page: </p> <div style="border:1px solid silver;border-image: initial; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> xmlns:live="using:Microsoft.Live.Controls"</pre> <!--CRLF--></div> </div> <p><em>Note: You will get “Invalid Markup” with the XAML provided because we are still in the developer preview stage. It is ok to proceed. </em></p> <p>The only thing to really pay attention to here is the <a href="http://msdn.microsoft.com/en-us/library/hh243646.aspx">Scopes</a> listed in the <strong>SignInButton</strong>. As you see we have three scopes. </p> <ol> <li>wl.signin - Single sign-in behavior. With <em>single sign-in</em>, users who are already signed in to Live Connect are also signed in to your website. </li> <li>wl.basic - Read access to a user's basic profile info. Also enables read access to a user's list of contacts. </li> <li>wl.contacts_create - Creation of new contacts in the user's address book. </li> </ol> <p>The first two are core and the last is an extended scope. We need all three of these to complete the requirements listed in this sample application. </p> <p>This will give us our user interface, now we will just need to wire up the event handlers which I am going to split up into two sections for learning purposes. </p> <h3>Coding Time – Basic User Information</h3> <p>Let’s go ahead and add in functionality to retrieve basic user information from the Live SDK. </p> <p>First, add the proper namespaces and then the following code:</p> <div style="border:1px solid silver;border-image: initial; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">using</span> Microsoft.Live;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">using</span> Microsoft.Live.Controls;</pre> <!--CRLF--></div> </div> <div style="border:1px solid silver;border-image: initial; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">private</span> LiveConnectClient liveClient;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">private</span> LiveConnectSession session;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">this</span>.btnLogIn.SessionChanged += btnLogIn_OnSessionChanged;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> btnLogIn_OnSessionChanged(<span style="color: #0000ff;">object</span> sender, LiveConnectSessionChangedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">if</span> (e.Session != <span style="color: #0000ff;">null</span> && e.Status == LiveConnectSessionStatus.Connected)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">this</span>.liveClient = <span style="color: #0000ff;">new</span> LiveConnectClient(e.Session);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> session = e.Session;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;">this</span>.liveClient.GetCompleted += OnGetCompleted;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;">this</span>.liveClient.GetAsync(<span style="color: #006080;">"me"</span>, <span style="color: #0000ff;">null</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> <span style="color: #0000ff;">this</span>.liveClient = <span style="color: #0000ff;">null</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> OnGetCompleted(<span style="color: #0000ff;">object</span> sender, LiveOperationCompletedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> { </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span> <span style="color: #0000ff;">if</span> (e.Error == <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> 28:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> 29:</span> dynamic result = e.Result;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> 30:</span> <span style="color: #0000ff;">this</span>.tbName.Text = <span style="color: #006080;">"Hello, "</span> + result.first_name + <span style="color: #006080;">" "</span> + result.last_name;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> 31:</span> <span style="color: #0000ff;">this</span>.tbGender.Text = <span style="color: #006080;">"You are a "</span> + result.gender + <span style="color: #006080;">" that lives in "</span> + result.locale + <span style="color: #006080;">"."</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> 32:</span> <span style="color: #0000ff;">this</span>.tbLiveProfile.Text = <span style="color: #006080;">"Your Live Profile can be found at: "</span> + result.link;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> 33:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> 34:</span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> 35:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> 36:</span> <span style="color: #0000ff;">this</span>.tbError.Text = e.Error.ToString();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> 37:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> 38:</span> }</pre> <!--CRLF--></div> </div> <p>The first thing that you will notice is that we are creating an event hander to handle the SessionChanged event. If we can determine that the session is valid then we create a new LiveConnectClient and pass the session to our LiveConnectSession. We finish up with adding an event handler on GetCompleted which will return the current users profile information and display them in our TextBlocks. Finally, we are outputting all errors to the tbError TextBlock. </p> <h3>Coding Time – Adding a new contact to the users Address Book</h3> <p>Let’s go ahead and add in functionality to add a new contact to the users address book from the Live SDK. </p> <div style="border:1px solid silver;border-image: initial; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> btnCreateContact_Click(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> LiveConnectClient createContact = <span style="color: #0000ff;">new</span> LiveConnectClient(session);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> var contact = <span style="color: #0000ff;">new</span> Dictionary<<span style="color: #0000ff;">string</span>, <span style="color: #0000ff;">object</span>>();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> contact.Add(<span style="color: #006080;">"first_name"</span>, <span style="color: #006080;">"Michael"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> contact.Add(<span style="color: #006080;">"last_name"</span>, <span style="color: #006080;">"Crump"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> createContact.PostCompleted +=</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">new</span> EventHandler<LiveOperationCompletedEventArgs>(CreateContactProperties_PostCompleted);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> createContact.PostAsync(<span style="color: #006080;">"me/contacts"</span>, contact);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">void</span> CreateContactProperties_PostCompleted(<span style="color: #0000ff;">object</span> sender, LiveOperationCompletedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;">if</span> (e.Error == <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> tbName.Text = <span style="color: #006080;">"Contact"</span> +</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span> e.Result[<span style="color: #006080;">"first_name"</span>].ToString() + <span style="color: #006080;">" "</span> + e.Result[<span style="color: #006080;">"last_name"</span>].ToString() +</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #006080;">" created with ID "</span> +</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> e.Result[<span style="color: #006080;">"id"</span>].ToString();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span> tbError.Text = <span style="color: #006080;">"Error calling API: "</span> + e.Error.ToString();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span> }</pre> <!--CRLF--></div> </div> <p>In this sample, we are creating a new <strong>LiveConnectClient</strong> by passing into it the existing session. Once that is complete, we create a Dictionary and add the users first_name and last_name to the dictionary. Finally, we create a PostCompleted event handler which will return the textblock with a success or an error. The last call is PostAsync which send a POST request to the specified Uri as an asynchronous operation.</p> <h3>Ready for Liftoff</h3> <p>Go ahead and build the application and run it. </p> <p>When you first hit the “Sign In” Button you will be presented with the following: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/____7_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="7" alt="7" src="http://www.silverlightshow.net/Storage/Users/mbcrump/____7_thumb.png" width="504" height="429" /></a></p> <p><em>Note: After you accept this one time then you will not have to accept it again.</em> </p> <p>Notice that the application says it you allow access then it will view your profile and contact list. The application automatically knows which features it is using by the scopes defined earlier. </p> <p>Select “Yes” and then click the “Sign In Button” located on the right hand side of your screen and you should see your Live profile information as long as you have filled it out in Windows Live. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/____9_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="9" alt="9" src="http://www.silverlightshow.net/Storage/Users/mbcrump/____9_thumb.png" width="326" height="219" /></a></p> <p>Next, click on the Create Contact button and you should see the following: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/____10_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="10" alt="10" src="http://www.silverlightshow.net/Storage/Users/mbcrump/____10_thumb.png" width="411" height="63" /></a></p> <p>You can now sign into your <a href="http://explore.live.com/">Windows Live</a> account and select “Contacts” under “Hotmail” and see the contact that you just created. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/____11_2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="11" alt="11" src="http://www.silverlightshow.net/Storage/Users/mbcrump/____11_thumb.png" width="541" height="208" /></a></p> <p>Congratulations, you have now completed your first Metro application using XAML/C# that uses the Live SDK. </p> <h3>Conclusion</h3> <p>At this point, we have seen how you would create a Windows 8 Metro application using XAML/C# that retrieves and updates information from the Live SDK. Now that you are equipped with the basics of using the Live SDK, I encourage you to explore the Live SDK further as we have barely scratched the surface. I want to thank you for reading this article and if you ever have any questions feel free to contact me on the various sources listed below. I also wanted to thank SilverlightShow for giving me the opportunity to share this information with everyone. </p> <p>You can follow Michael on Twitter at <a href="http://www.twitter.com/mbcrump">mbcrump</a> or keep up with his blog by visiting <a href="http://michaelcrump.net/">michaelcrump.net</a>.</p> http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx editorial@silverlightshow.net (Michael Crump ) http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx#comments http://www.silverlightshow.net/items/Using-the-Live-SDK-in-Windows-8-XAML-C-Metro-Applications.aspx Mon, 30 Jan 2012 03:59:34 GMT 10 Laps around Silverlight 5 (Part 9 of 10) <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;">This article is sponsored by <a href="http://ads.silverlightshow.net/a.aspx?ZoneID=53&Task=Click&Mode=HTML&SiteID=1&PageID=95076" target="_blank"><img alt="" width="0" height="0" style="border:0px solid;" src="http://ads.silverlightshow.net/a.aspx?ZoneID=53&Task=Get&Mode=HTML&SiteID=1&PageID=95076" />Telerik RadControls for Silverlight</a>. Related content from the sponsor: <p><strong>Check out what's new!</strong></p> <ul> <li><span style="font-size: 14px;"><a href="http://ads.silverlightshow.net/a.aspx?ZoneID=52&Task=Click&Mode=HTML&SiteID=1&PageID=80309" target="_blank"> <img alt="" src="http://ads.silverlightshow.net/a.aspx?ZoneID=52&Task=Get&Mode=HTML&SiteID=1&PageID=80309" width="0" height="0" style="border-width: 0px;border-style: solid;" />The latest Q3 2011 release is out! Introducing ChartView & next-generation CRM demo with full source code.</a></span> </li> <li><span style="font-size: 14px;"><a href="http://ads.silverlightshow.net/a.aspx?ZoneID=51&Task=Click&Mode=HTML&SiteID=1&PageID=78027" target="_blank"> <img alt="" src="http://ads.silverlightshow.net/a.aspx?ZoneID=51&Task=Get&Mode=HTML&SiteID=1&PageID=78027" width="0" height="0" style="border-width: 0px;border-style: solid;" />Introducing Telerik Patterns & Practices for Silverlight and WPF.</a></span> </li> <li><span style="font-size: 14px;"><a href="http://ads.silverlightshow.net/a.aspx?ZoneID=54&Task=Click&Mode=HTML&SiteID=1&PageID=39336" target="_blank"> <img alt="" src="http://ads.silverlightshow.net/a.aspx?ZoneID=54&Task=Get&Mode=HTML&SiteID=1&PageID=39336" width="0" height="0" style="border-width: 0px;border-style: solid;" />The RadControls for Silverlight has been redesigned. Explore it!</a></span></li> </ul> </div> <table width="20"> <tbody> <tr> <td> <div class="fb-like" data-show-faces="true" data-send="false" data-href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx" data-font="segoe ui" data-layout="button_count"></div> </td> <td><a href="https://twitter.com/share" class="twitter-share-button" data-via="silverlightshow" data-counturl="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx" data-count="horizontal" data-text="Reading SilverlightShow article '10 Laps around Silverlight 5 (Part 9 of 10)' by @mbcrump #sl5" data-url="http://slshow.net/uprKjj">Tweet</a></td> <td><g:plusone size="medium" href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx"></g:plusone> </td> <td> </td> </tr> </tbody> </table> <p> <br /> To contact me directly please visit my blog at <a href="http://michaelcrump.net/">http://michaelcrump.net/</a> or through twitter at <a href="http://twitter.com/mbcrump">http://twitter.com/mbcrump</a>.</p> <p>This article is Part 9 of the series “10 Laps around Silverlight 5.” If you have missed any other section then please see the Roadmap below. </p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More resources...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/news/Free-Silverlight-Show-Webinar-Introduction-to-XAML-Development-on-Windows-8.aspx">Upcoming Webinar on Nov 29th: XAML Development on Windows 8</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/GetStarted.aspx">Get Started with Silverlight 4</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx">'Getting Ready for Microsoft Silverlight Exam 70-506' Ebook </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx"><img style="border:0px solid;" alt="Getting Ready for Microsoft Silverlight Exam 70-506: Ebook" src="http://www.silverlightshow.net/Storage/sl_exam_thumb_small.png" usemap="#rade_img_map_1291385581316" /></a></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> To refresh your memory on what Silverlight is: <p>Microsoft Silverlight is an application framework for writing Rich Internet Applications. </p> The run-time environment is available as a plug-in for most web browsers and works on a variety of operating systems including Windows, Mac and Linux. <p>To recap what we learned in the previous section:</p> <ul> <li>We learned how you could specify a break point inside of a Binding Expressions in Silverlight 5. We also learned how you could use this same functionality inside of Silverlight 4 with the Silverlight 5 SDK installed. </li> <li>We then created a sample application that contained a binding error and took a look at the output window to investigate it further.  We were then able to determine the source of our error and correct it quickly. </li> <li>We finished up with listing several productivity and performance improvements in Silverlight 5 that you may not be aware of. </li> </ul> <p>In this article, I am going to discuss several new features/controls such as Double and Triple click support, PivotViewer and ComboBox Type-Ahead. Please review the Roadmap for the series before going any further.</p> <h3>The Roadmap for this Series</h3> <p>I’ve included the Roadmap for the series below as you may want to visit other sections as you learn Silverlight 5. I picked the following features as I thought that you may find them useful in your day-to-day work. If you want a specific topic covered then please leave it in the comments below.</p> <p>1) <strong><a href="http://www.silverlightshow.net/items/Silverlight-5-Part-1-of-10.aspx">Introduction to SL5 – This post which provides a brief history of Silverlight and relevant links.</a></strong> </p> <p>2) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-2-of-10.aspx">Binding- Ancestor Relative Source Binding and Implicit Data Templates.</a></strong></p> <p>3) <strong></strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-3.aspx" target="_self"><strong>Graphics</strong> <strong>–XNA 3D API and Improved Graphics Stack</strong>.</a></p> <p>4) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-4-of-10.aspx">Media - Low-Latency Sound using XNA and Remote Control and Media Command (Keys) Support.</a></strong></p> <p>5) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-5-of-10.aspx">Text - Text Tracking and Leading, Linked and Multi-column Text, OpenType Support, Pixel Snapped Text and TextOptions.</a></strong></p> <p>6) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-6-of-10.aspx">Operating System Integration - Part 1 - P/Invoke, Multiple Windows and Unrestricted File System Access in Full Trust.</a></strong></p> <p>7) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-7-of-10.aspx">Operating System Integration - Part 2 - Default Filename for SaveFileDialog, 64-bit browser support and Power Awareness.</a></strong></p> <p>8) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-8-of-10.aspx">Productivity and Performance - XAML Binding Debugging, Parser Performance Improvements and Multi-core JIT for improved start-up time.</a></strong></p> <p>9) <strong>Controls</strong> <strong>[This post] </strong>- Double and Triple click support, PivotViewer and ComboBox Type-Ahead.</p> <p>10) <strong><a href="http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-10-of-10.aspx" target="_self">Other items - In-Browser HTML, PostScript and Tasks for TPL</a>.</strong></p> <h3></h3> <h3>Double and Triple Click Support</h3> <p>One of the new features in Silverlight 5 is the ability to use Double and Triple Click Support. This functionality will tell you how many times the user has clicked the mouse button. The property is called <strong>ClickCount</strong> and resides in the <strong>MouseButtonEventArgs</strong> class. Let’s take a look at how to use this new feature.</p> <p>Fire up a new Silverlight 5 project and give it any name that you want. </p> <p>Switch over to the MainPage.xaml.cs and add the following code:<em> (Note: You may not need the MainPage() Method section)</em></p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> textBlock1_MouseLeftButtonDown(<span style="color: #0000ff;">object</span> sender, MouseButtonEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> textBlock1.Text = e.ClickCount.ToString();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> }</pre> <!--CRLF--></div> </div> <p>Switch back over to the MainPage.xaml and add in the following code replacing the current Grid:</p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="Black"</span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="52,49,68,74"</span> <span style="color: #ff0000;">CornerRadius</span><span style="color: #0000ff;">="10"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="152"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="textBlock1"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="244"</span> <span style="color: #ff0000;">MouseLeftButtonDown</span><span style="color: #0000ff;">="textBlock1_MouseLeftButtonDown"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="#FFFF2E2E"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="96"</span> <span style="color: #ff0000;">TextAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>If we go ahead and run the application then we will see the following application. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/SNAGHTMLf7d6db1.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;border-style: solid;" title="SNAGHTMLf7d6db1" alt="SNAGHTMLf7d6db1" src="http://www.silverlightshow.net/Storage/Users/mbcrump/SNAGHTMLf7d6db1_thumb.png" width="304" height="268" /></a></p> <p>Go ahead and begin clicking inside of the border and you will see the number increase. If you wait a few seconds and click again then you will notice that it reset itself. You could easily add If..Then… statements to determine what click count number they are on. This may be helpful for a 35 click Easter egg. :) </p> <h3>PivotViewer</h3> <br /> <p>Microsoft’s Silverlight Team defines it as the following: The Silverlight PivotViewer makes it easier to interact with massive amounts of data on the web in ways that are powerful, informative and valuable. I couldn’t agree more. Let’s begin today by creating a SL5 application that uses the PivotViewer control and displays a collection of movies. <em>(Classic example, eh?) </em></p> <p>Please note that this sample barely scratches the surface of what you can do with the PivotViewer. I’ve included an official link by Microsoft at the bottom of this post for your reference. </p> <p>Fire up a new Silverlight 5 project and give it the name <strong>PivotViewer</strong>. </p> <p>Switch over to the MainPage.xaml.cs and add the following code:</p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">using</span> System;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">using</span> System.Linq;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">using</span> System.Windows;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">using</span> System.Windows.Controls;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">using</span> System.Collections.ObjectModel;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">namespace</span> PivotViewer</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> MainPage : UserControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;">private</span> ObservableCollection<Movie> Movies;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> Movies = <span style="color: #0000ff;">new</span> ObservableCollection<Movie>();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;">for</span> (Int64 i = 0; i < 500; i++)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span> Movies.Add(<span style="color: #0000ff;">new</span> Movie() { Title = <span style="color: #006080;">"Ice Age"</span> + i.ToString(), CountryOfOrigin = <span style="color: #006080;">"USA"</span>, Description = <span style="color: #006080;">"Set during the Ice Age."</span>, Director = <span style="color: #006080;">"Chris Wedge"</span> + i.ToString(), Duration = 90, ReleaseDate = <span style="color: #0000ff;">new</span> DateTime(2002, 09, 03) });</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> UserControl_Loaded(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> MoviePivot.ItemsSource = Movies;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> 28:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> 29:</span> }</pre> <!--CRLF--></div> </div> <br /> We need to create a new class now named <strong>Movie</strong>.  Inside of that class should look like the following: <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">using</span> System;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">namespace</span> PivotViewer</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Movie</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Title { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Duration { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">public</span> DateTime ReleaseDate{ get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> CountryOfOrigin { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Description { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Director { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> }</pre> <!--CRLF--></div> </div> <p>Switch back over to the MainPage.xaml and add in the following code replacing everything:</p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x:Class</span><span style="color: #0000ff;">="PivotViewer.MainPage"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #ff0000;">xmlns:d</span><span style="color: #0000ff;">="http://schemas.microsoft.com/expression/blend/2008"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #ff0000;">xmlns:mc</span><span style="color: #0000ff;">="http://schemas.openxmlformats.org/markup-compatibility/2006"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #ff0000;">xmlns:pivot</span><span style="color: #0000ff;">="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #ff0000;">Loaded</span><span style="color: #0000ff;">="UserControl_Loaded"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #ff0000;">mc:Ignorable</span><span style="color: #0000ff;">="d"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #ff0000;">d:DesignHeight</span><span style="color: #0000ff;">="300"</span> <span style="color: #ff0000;">d:DesignWidth</span><span style="color: #0000ff;">="400"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewer</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="MoviePivot"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #ff0000;">AccentColor</span><span style="color: #0000ff;">="LightGreen"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="LightGray"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #ff0000;">ControlBackground</span><span style="color: #0000ff;">="LightYellow"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #ff0000;">SecondaryBackground</span><span style="color: #0000ff;">="AntiqueWhite"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #ff0000;">SecondaryForeground</span><span style="color: #0000ff;">="Bisque"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> <span style="color: #ff0000;">SecondaryItemValueBackgroundColor</span><span style="color: #0000ff;">="Chocolate"</span> <span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #008000;"><!--Setting PivotProperties--></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewer.PivotProperties</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewerStringProperty</span> <span style="color: #ff0000;">Id</span><span style="color: #0000ff;">="FTitle"</span> <span style="color: #ff0000;">Options</span><span style="color: #0000ff;">="CanFilter"</span> <span style="color: #ff0000;">DisplayName</span><span style="color: #0000ff;">="Title"</span> <span style="color: #ff0000;">Binding</span><span style="color: #0000ff;">="{Binding Title}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewerStringProperty</span> <span style="color: #ff0000;">Id</span><span style="color: #0000ff;">="FDuration"</span> <span style="color: #ff0000;">Options</span><span style="color: #0000ff;">="CanFilter"</span> <span style="color: #ff0000;">DisplayName</span><span style="color: #0000ff;">="Duration"</span> <span style="color: #ff0000;">Binding</span><span style="color: #0000ff;">="{Binding Duration}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewerStringProperty</span> <span style="color: #ff0000;">Id</span><span style="color: #0000ff;">="FDirector"</span> <span style="color: #ff0000;">Options</span><span style="color: #0000ff;">="CanFilter"</span> <span style="color: #ff0000;">DisplayName</span><span style="color: #0000ff;">="Director"</span> <span style="color: #ff0000;">Binding</span><span style="color: #0000ff;">="{Binding Director}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">pivot:PivotViewer.PivotProperties</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> 28:</span> <span style="color: #008000;"><!--Setting data--></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> 29:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewer.ItemTemplates</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> 30:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">pivot:PivotViewerItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> 31:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="200"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="200"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="CadetBlue"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> 32:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">Orientation</span><span style="color: #0000ff;">="Vertical"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> 33:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> 34:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">Orientation</span><span style="color: #0000ff;">="Horizontal"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> 35:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Title}"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> 36:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="("</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> 37:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Duration}"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> 38:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">=")"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum39" style="color: #606060;"> 39:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum40" style="color: #606060;"> 40:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Director}"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum41" style="color: #606060;"> 41:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding CountryOfOrigin}"</span> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum42" style="color: #606060;"> 42:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum43" style="color: #606060;"> 43:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum44" style="color: #606060;"> 44:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">pivot:PivotViewerItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum45" style="color: #606060;"> 45:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">pivot:PivotViewer.ItemTemplates</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum46" style="color: #606060;"> 46:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">pivot:PivotViewer</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum47" style="color: #606060;"> 47:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum48" style="color: #606060;"> 48:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>If we run our application and select a few items then we will see the following screen. As you can tell when the application loaded that it contained 500 items – Wow!</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/SNAGHTMLf991cf3.png"><img style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="SNAGHTMLf991cf3" alt="SNAGHTMLf991cf3" src="http://www.silverlightshow.net/Storage/Users/mbcrump/SNAGHTMLf991cf3_thumb.png" width="613" height="570" /></a></p> <p>I’ve barely touched on the amount of things that you can do with PivotViewer. If you want to learn more than please visit the Silverlight site located <a href="http://www.silverlight.net/learn/data-networking/pivot-viewer/pivotviewer-control">here</a>. </p> <h3>Combo-Box Type Ahead</h3> <br /> <p>Another new feature in Silverlight 5 is Combo-Box Type Ahead. This makes choosing items from a long list very simple. Let’s go ahead and build a sample application. </p> <p>Fire up a new Silverlight 5 project and give it any name that want. </p> <p>Switch over to the MainPage.xaml.cs and add the following code:</p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> var lstDevelopers = <span style="color: #0000ff;">new</span> List<<span style="color: #0000ff;">string</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> { <span style="color: #006080;">"Michael Crump"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #006080;">"Pete Brown"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #006080;">"Victor G."</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #006080;">"Scott Hanselman"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #006080;">"Jesse Liberty"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #006080;">"Shawn Wildermuth"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #006080;">"Scott Gu"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #006080;">"Joel Cochran"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #006080;">"Kunal Chowdhurry"</span> };</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> cbDevelopers.ItemsSource = lstDevelopers;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> }</pre> <!--CRLF--></div> </div> <p>Switch back over to the MainPage.xaml and add in the following code replacing the existing Grid:</p> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ComboBox</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="cbDevelopers"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="40"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="150"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="20"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>If we run our application and begin typing then we will see the ComboBox automatically highlights the row that matches our selection.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/_image_2.png"><img style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="image" alt="image" src="http://www.silverlightshow.net/Storage/Users/mbcrump/_image_thumb.png" width="161" height="240" /></a></p> <h3>Conclusion</h3> <p>At this point, we have seen how you would use Double and Triple click support, PivotViewer and ComboBox Type-Ahead. In the next and final part of the series, I am going to take a look at several new features shipping with Silverlight 5 including : In-Browser HTML, PostScript and Tasks for TPL.  Again, thanks for reading and please come back for the next part.</p> <p>To contact me directly please visit my blog at <a href="http://michaelcrump.net/">http://michaelcrump.net/</a> or through twitter at <a href="http://twitter.com/mbcrump">http://twitter.com/mbcrump</a>.</p> http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx editorial@silverlightshow.net (Michael Crump ) http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx#comments http://www.silverlightshow.net/items/10-Laps-around-Silverlight-5-Part-9-of-10.aspx Tue, 06 Dec 2011 12:27:00 GMT Telerik XAML Q3'11 Beta - Walkthrough (Part 3 of 3) <p style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;">This is part 3 from an article on <strong>Telerik XAML Q3'11 Beta</strong>, authored by Michael Crump and Evan Hutnick. <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-1-of-3.aspx" target="_self">Part 1</a> | <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-2-of-3.aspx" target="_self">Part 2</a><br /> <br /> SilverlightShow will occasionally publish content related to 3rd party Silverlight and Windows Phone products, following interest and article topic requests received through our surveys and feedback email (editorial at silverlightshow dot net).<br />   </p> <p>In this third and last installment of <em>Telerik XAML Q3’11 Beta—A Walkthrough</em> we discuss RadChartView. If you read Parts 1 and 2, you’ll know that in the upcoming mid-November release, in addition to RadBarCode and RadVirtualizingWrapPanel, our XAML team here at Telerik is including an exciting and brand-new control: RadChartView. If you haven’t read Parts 1 and 2, you should. All good news aside, we recommend you <a href="http://www.telerik.com/account/your-products/trial-product-versions/trial-single-download.aspx?pmvid=2574&pid=571&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_download-SL-beta-1"><span style="font-family: 'segoe ui'; font-size: 13px;">download the beta</span></a> so you make the most of this and the other two articles.</p> <p> </p> <h4>Introducing RadChartView</h4> <p>For anyone who has read the <a href="http://blogs.telerik.com/blogs/posts/11-10-21/telerik-xaml-controls-q3-2011-beta-introduces-new-and-empowered-chartingkit.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_Beta-announcement-1">XAML Beta announcement</a> or been working with the RadControls for Windows Phone, the look and feel of RadChartView may seem a bit familiar… because it is! Our Data Visualization and Windows Phone teams have collaborated to create a charting engine that is 100% view-agnostic, meaning it powers RadChart for Windows Phone, the new RadChartView, and is the base of our <a href="http://blogs.telerik.com/blogs/posts/11-09-29/radchart-for-windows-8-winrt-a-prelude.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_RadChart-WinRT-post">proof-of-concept RadChart for WinRT</a>. The end result is a fast, versatile, and highly optimized charting solution that works across platforms and lives up to the Telerik XAML standard of maintaining the same code-base and API while providing powerful cross-platform charting. Read on as we dive into the new RadChartView Beta and explore some of the functionality that we have available today.</p> <p><a href="http://demos.telerik.com/silverlight/beta/#ChartingKit/FirstLook">You can also check out the RadChartView demos here</a>.</p> <h4> </h4> <h4>Getting Started</h4> <p>Before we actually get to the good stuff, there are just a few preliminary steps to properly set things up—we’ll be into code soon enough, don’t worry!</p> <p>First, <a href="http://www.telerik.com/account/your-products/trial-product-versions/trial-single-download.aspx?pmvid=2574&pid=571&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_download-SL-beta-2">make sure you download the RadControls for Silverlight Q3 Beta trial to gain access to the new RadChartView</a>. Once it is installed, open up Visual Studio 2010 and create a new Silverlight project. With your new project loaded, add the following assemblies from your installed directory for the Telerik Silverlight tools:</p> <ul> <li>Telerik.Windows.Controls </li> <li>Telerik.Windows.Controls.Chart </li> <li>Telerik.Windows.Data </li> </ul> <p>Finally, add the Telerik namespace:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> xmlns:telerik=<span style="color: #006080;">"http://schemas.telerik.com/2008/xaml/presentation"</span> </pre> <!--CRLF--></div> </div> <p>As well as a few rows and columns in our default LayoutRoot grid:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.RowDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.RowDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p><span style="font-size: 13px;"><em>Now</em>… let’s get started. <img class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://www.silverlightshow.net/Storage/Users/EvanHutnick/wlEmoticon-winkingsmile_2.png" /></span></p> <h4>Setting the Bar</h4> <p>The “Hello World” of any charting solution is creating a bar chart. This is one of the standard types of charts used across every industry and vertical to show data. So, if a chart can accomplish it with ease we’re off to a good start. Assuming you may be familiar with the previous RadChart from our Silverlight and WPF suites, we are going to walk through the steps to set up a simple bar chart and explain along the way the architecture behind ChartView.</p> <p>The first step is to define a RadCartesianChart – a new term for anyone familiar with the previous RadChart. One of the issues that we ran into with RadChart is that when we tried to continue adding functionality to an existing solution, it became obvious that it’s not something that scales well. We need to add more advanced functionality or functionality based on a particular scenario. RadChartView takes a different approach and instead works on an opt-in model for including different functionality. This also means we can separate out different charting scenarios to ensure our code is as clean and optimized for each type of chart as possible. Anytime you’re working with a standard X-Y axis, you’ll use RadCartesianChart:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="xRadCartesianChart"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Of course, before we go any further we’ll need a bit of data to display in our charts. Rather than explain in fine-grained detail how I’m putting together sample data, here’s a quick copy-and-pasteable chunk of code you can use to create a collection of five ObservableCollections of our SampleChartDataItem:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> MainPage : UserControl</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">public</span> ObservableCollection<ObservableCollection<SampleChartDataItem>> chartData { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060;"> 7:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060;"> 8:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">this</span>.DataContext = <span style="color: #0000ff;">this</span>;</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060;"> 10:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;">this</span>.Loaded += <span style="color: #0000ff;">new</span> RoutedEventHandler(MainPage_Loaded);</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum12" style="color: #606060;"> 12:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum13" style="color: #606060;"> 13:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">void</span> MainPage_Loaded(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum15" style="color: #606060;"> 15:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum16" style="color: #606060;"> 16:</span> Random rnd = <span style="color: #0000ff;">new</span> Random();</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum17" style="color: #606060;"> 17:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum18" style="color: #606060;"> 18:</span> chartData = <span style="color: #0000ff;">new</span> ObservableCollection<ObservableCollection<SampleChartDataItem>>();</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum19" style="color: #606060;"> 19:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> y = 0; y < 5; y++)</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum21" style="color: #606060;"> 21:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum22" style="color: #606060;"> 22:</span> var tempCollection = <span style="color: #0000ff;">new</span> ObservableCollection<SampleChartDataItem>();</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum23" style="color: #606060;"> 23:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i < 20; i++)</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum25" style="color: #606060;"> 25:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum26" style="color: #606060;"> 26:</span> tempCollection.Add(<span style="color: #0000ff;">new</span> SampleChartDataItem()</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum27" style="color: #606060;"> 27:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum28" style="color: #606060;"> 28:</span> id = i,</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum29" style="color: #606060;"> 29:</span> value1 = rnd.NextDouble() * 20,</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum30" style="color: #606060;"> 30:</span> value2 = rnd.NextDouble() * 40 * (i % 2 == 0 ? 1 : -1),</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum31" style="color: #606060;"> 31:</span> angle = i * 18,</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum32" style="color: #606060;"> 32:</span> dtvalue = DateTime.Today.AddDays(i)</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum33" style="color: #606060;"> 33:</span> });</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum34" style="color: #606060;"> 34:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum35" style="color: #606060;"> 35:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum36" style="color: #606060;"> 36:</span> chartData.Add(tempCollection);</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum37" style="color: #606060;"> 37:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum38" style="color: #606060;"> 38:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum39" style="color: #606060;"> 39:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum40" style="color: #606060;"> 40:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum41" style="color: #606060;"> 41:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> SampleChartDataItem</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum42" style="color: #606060;"> 42:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum43" style="color: #606060;"> 43:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> id { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum44" style="color: #606060;"> 44:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">double</span> value1 { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum45" style="color: #606060;"> 45:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">double</span> value2 { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum46" style="color: #606060;"> 46:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">double</span> angle { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum47" style="color: #606060;"> 47:</span> <span style="color: #0000ff;">public</span> DateTime dtvalue { get; set; }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum48" style="color: #606060;"> 48:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum49" style="color: #606060;"> 49:</span> <span style="color: #0000ff;">public</span> SampleChartDataItem()</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum50" style="color: #606060;"> 50:</span> {</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum51" style="color: #606060;"> 51:</span>  </pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum52" style="color: #606060;"> 52:</span> }</pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum53" style="color: #606060;"> 53:</span> }</pre> <!--CRLF--></div> </div> <p> </p> <p><span style="font-family: 'courier new';"></span></p> With that out of the way, we can now turn that data into something visible... <p> </p> <p>Working with a bar series is relatively easy – the three main things we need to work with are ValueBinding, CategoryBinding, and ItemsSource. In most scenarios consider ValueBinding as your Y-Axis and CategoryBinding as your X-Axis, so here we’ll use value1 for Y and id for X, along with the first collection in chartData to populate some data:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:BarSeries</span> <span style="color: #ff0000;">ValueBinding</span><span style="color: #0000ff;">="value1"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">CategoryBinding</span><span style="color: #0000ff;">="id"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding chartData[0]}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p>Before running it, however, we need to define the Axis being used. As I previously mentioned, RadChart assumes nothing and allows you to use the exact pieces you need for displaying data. Since I am using categorical data (id) and value-based data that works on a linear scale (value1), I can use a LinearAxis for Y and a CategoricalAxis for X:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.HorizontalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:CategoricalAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.HorizontalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.VerticalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:LinearAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.VerticalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p><span style="font-size: 13px;"><em>Now</em> we can run our application and see the following:</span></p> <p><img alt="" src="http://blogs.telerik.com/Libraries/Silverlight_team/chartingkit-1.sflb" /></p> <p><span style="font-size: 13px;">If we wanted to add another series, we could simply copy and paste our first bar series declaration and instead use chartData[1] to view different data, switching to value2 instead of value1. Before running though, go ahead and modify your Cartesian Chart declaration to look like so:</span></p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="xRadCartesianChart"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">Palette</span><span style="color: #0000ff;">="Metro"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>This will allow RadChartView to assign colors based on the current palette; otherwise both series would look the same. Now running will produce the following:</p> <p><img alt="" src="http://blogs.telerik.com/Libraries/Silverlight_team/chartingkit-2.sflb" /></p> <p>Much nicer! But, of course, now that we are showing a few different types of data it would be ideal if we could display axis and grid lines to make this a bit easier on our eyes. We accomplish this detail by using the Cartesian Chart Grid, containing all the settings for axis display within the charting surface. In our case, we’ll set the MajorLinesVisibility to XY, MajorXLineDashArray to ‘2 0’, MajorXLinesRenderMode to Inner, and StripLinesVisibility to XY. In code this looks something like the following:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:CartesianChartGrid</span> <span style="color: #ff0000;">MajorLinesVisibility</span><span style="color: #0000ff;">="XY"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">MajorXLineDashArray</span><span style="color: #0000ff;">="2 0"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #ff0000;">MajorXLinesRenderMode</span><span style="color: #0000ff;">="Inner"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #ff0000;">StripLinesVisibility</span><span style="color: #0000ff;">="XY"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>With our newly set grid, we can see the chart as follows – a much better representation that is fully customized to meet our requirements without unnecessary extras, thus ensuring high performance:</p> <p><img alt="" src="http://blogs.telerik.com/Libraries/Silverlight_team/chartingkit-3_1.sflb" /></p> <p><span style="font-size: 13px;">Now <em>that</em> is a sweet-looking chart. </span><a href="http://blogs.telerik.com/Libraries/Evan_s_Documents/RadChartingKitBetaDemos.sflb?download=true&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_download-source-code-Evan-1">>Get the full code here</a>.</p> <h4>Feeling a bit Scattered?</h4> <p>Don’t worry—it’s about this easy to do everything with RadChartView. Take, for example, a ScatterPoint chart in the new format. As it turns out, courtesy of our previous exercise, we have a bit of code that we can re-use from our other RadCartesianChart. Give it a new name, change rows, remove the bar references, modify the horizontal axis, and you’ve got the basis for a new chart:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="xRadChart2"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Row</span><span style="color: #0000ff;">="1"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">Palette</span><span style="color: #0000ff;">="Metro"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.HorizontalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:LinearAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.HorizontalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.VerticalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:LinearAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.VerticalAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadCartesianChart.Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:CartesianChartGrid</span> <span style="color: #ff0000;">MajorLinesVisibility</span><span style="color: #0000ff;">="XY"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #ff0000;">MajorXLineDashArray</span><span style="color: #0000ff;">="2 0"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #ff0000;">MajorXLinesRenderMode</span><span style="color: #0000ff;">="Inner"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #ff0000;">StripLinesVisibility</span><span style="color: #0000ff;">="XY"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart.Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadCartesianChart</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>To add a new ScatterPoint series, we add a new ScatterPointSeries and set out xValueBinding to value1, YValueBinding to value2, and itemssource to chartData[2]—see below:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:ScatterPointSeries</span> <span style="color: #ff0000;">XValueBinding</span><span style="color: #0000ff;">="value1"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">YValueBinding</span><span style="color: #0000ff;">="value2"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding chartData[2]}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:ScatterPointSeries.PointTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Ellipse</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="5"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="5"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Red"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:ScatterPointSeries.PointTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:ScatterPointSeries</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Going with the Cartesian Chart theme, the XValue runs along the horizontal axis while the YValue runs along the vertical axis. ItemsSource works as expected to supply the binding with data. Before we run it again, we’ll set a PointTemplate to make our ScatterPoint data really stand out. This will be a basic shape for right now, but since this is a DataTemplate we can always utilize bindings and converters to customize our chart.</p> <p>Once this code is in place, you brand new Scatter Point chart will look something like the following:</p> <p><img alt="" src="http://blogs.telerik.com/Libraries/Silverlight_team/chartingkit-4.sflb" /></p> <p>Simple and easy!</p> <h4>Polar Chart, Anybody?</h4> <p>One chart type notoriously missing from the previous RadChart implementation was a Polar chart. Thankfully the architecture of RadChartView has solved that dilemma with RadPolarChart, an alternative to the Cartesian Chart that offers data displayed in a polar format.</p> <p>To start, we’ll add a new RadPolarChart to our XAML with a few properties set:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadPolarChart</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="xPolarChart"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">RowSpan</span><span style="color: #0000ff;">="2"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #ff0000;">Palette</span><span style="color: #0000ff;">="Metro"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadPolarChart</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>The next step is to define several series on our chart. For Polar charts we have PolarAreaSeries, PolarLineSeries, and PolarPointSeries, all of which take the same properties to display data – AngleBinding, ValueBinding, and of course ItemsSource. We also want to define a template for our PointSeries, this time my choice being a Rectangle. These series definitions are as follows:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:PolarAreaSeries</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding chartData[2]}"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">AngleBinding</span><span style="color: #0000ff;">="angle"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">ValueBinding</span><span style="color: #0000ff;">="value1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:PolarLineSeries</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding chartData[3]}"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #ff0000;">AngleBinding</span><span style="color: #0000ff;">="angle"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #ff0000;">ValueBinding</span><span style="color: #0000ff;">="value1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:PolarPointSeries</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding chartData[4]}"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #ff0000;">AngleBinding</span><span style="color: #0000ff;">="angle"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #ff0000;">ValueBinding</span><span style="color: #0000ff;">="value1"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:PolarPointSeries.PointTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Orange"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:PolarPointSeries.PointTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:PolarPointSeries</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>And of course, to give the data somewhere to display we will set our PolarAxis and RadialAxis:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; width: 97.5%; line-height: 12pt; overflow-x: auto; overflow-y: auto; font-family: 'courier new', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4; text-align: left;"> <div id="codeSnippet" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4; text-align: left;"> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadPolarChart.PolarAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:PolarAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadPolarChart.PolarAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadPolarChart.RadialAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:NumericRadialAxis</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow-x: visible; overflow-y: visible; font-family: 'courier new', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">telerik:RadPolarChart.RadialAxis</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>End result? A beautiful looking Polar chart with crisp, clean rendering:</p> <p><img alt="" src="http://blogs.telerik.com/Libraries/Silverlight_team/chartingkit-5.sflb" /></p> <h4>Wrapping Up</h4> <p>Hopefully this walkthrough has helped you to understand and appreciate the new architecture behind RadChartView. Be sure to <a href="http://blogs.telerik.com/Libraries/Evan_s_Documents/RadChartingKitBetaDemos.sflb?download=true&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_download-source-code-Evan-2">download the source code for this article</a> and check out the <a href="http://blogs.telerik.com/blogs/posts/11-10-21/telerik-xaml-controls-q3-2011-beta-introduces-new-and-empowered-chartingkit.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_Beta-announcement-2">RadControls for Silverlight and WPF Q3 2011 Beta release</a> right now, if you haven’t already—let us know what you think! You can also look forward to more helpful walkthroughs covering what’s new in the Q3 2011 Telerik XAML releases so stay tuned!</p> <p style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in;"><em>If you enjoyed this article, you can </em><a href="http://blogs.telerik.com/evanhutnick/posts.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-3_Evan-Hutnick-Telerik-blog"><em>find similar postings by XAML Evangelist, Evan Hutnick, on his Telerik blog</em></a><em>.</em></p> http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-3-RadChartingKit.aspx editorial@silverlightshow.net (Evan Hutnick ) http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-3-RadChartingKit.aspx#comments http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-3-RadChartingKit.aspx Wed, 02 Nov 2011 19:42:00 GMT Telerik XAML Q3'11 Beta - Walkthrough (Part 2 of 3) <p style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;">This is part 2 from an article on <strong>Telerik XAML Q3'11 Beta</strong>, authored by Michael Crump and Evan Hutnick. <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-1-of-3.aspx" target="_self">Part 1</a> | <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-3-RadChartingKit.aspx" target="_self">Part 3</a><br /> <br /> SilverlightShow will occasionally publish content related to 3rd party Silverlight and Windows Phone products, following interest and article topic requests received through our surveys and feedback email (editorial at silverlightshow dot net).<br />   </p> <h4>Telerik XAML Q3’11 Beta</h4> <p><strong>Part 2: RadVirtualizingWrapPanel</strong></p> <p>Here begins the second of three parts, which comprise <em>Telerik XAML Q3’11 Beta—A Walkthrough</em>. In case you missed it, with Telerik’s upcoming release in November you’ll have access to the official versions of RadBarCode, RadVirtualizingWrapPanel, and RadChartView—and a few sure-to-please extras. </p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More resources...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Upcoming SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/GetStarted.aspx">Get Started with Silverlight 4</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/sl_authorization.aspx">'Authorization in Silverlight' Ebook </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/sl_authorization.aspx"><img style="border:0px solid;width: 107px; height: 141px;" alt="Authorization in Silverlight: Ebook" src="http://www.silverlightshow.net/Storage/Ebooks/authorization_sl_cover.png" usemap="#rade_img_map_1291385581316" /></a></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> To make the most of these articles, <a href="http://www.telerik.com/account/your-products/trial-product-versions/trial-single-download.aspx?pmvid=2574&pid=571&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-2_download-SL-beta">download the beta</a> before getting started. <p> </p> <p>For an introductory overview you can <a href="http://blogs.telerik.com/blogs/posts/11-10-21/telerik-xaml-controls-q3-2011-beta-introduces-new-and-empowered-chartingkit.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-2_Beta-announcement">check out the official beta announcement on our Silverlight Team blog</a>.</p> <h4>Introducing RadVirtualizingWrapPanel</h4> <p><strong>RadVirtualizingWrapPanel</strong> boosts the performance of your ListBox/ListView when binding to a large amount of data. The control generates only the visible items and positions them in sequential order from left to right and top to bottom, breaking the content to the next line at the edge of its containing box.</p> <p><a href="http://demos.telerik.com/silverlight/beta/#VirtualizingWrapPanel/FirstLook">You can check out the VisualizingWrapPanel demos here.</a></p> <h4> </h4> <h4>Let’s Get Buildin’</h4> <p>We are going to build a sample project that uses Telerik’s <strong>RadVirtualizingWrapPanel</strong> in Silverlight 4. </p> <p>Open Visual Studio 2010, select a new Silverlight Project, and give it any name that you want. In this example, I have given it the name of <strong>RadVirtualizingWrapPanelSample</strong>.</p> <p>We are going to need to add one reference to our project in order to use Telerik’s VirtualizingWrapPanel. </p> <ol> <li>Telerik.Windows.Controls </li> </ol> <p><em>Make sure that you select 2011.3.1020.1040 as the version number to ensure that you are using the Q3 Beta release.</em></p> <p>With the reference added, we now need to add the Telerik XAML Namespace to our <strong>MainPage.xaml</strong> file. Simply double-click the MainPage.xaml file and add the following code snippet:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"</pre> <!--CRLF--></div> </div> <p>Now, we must add a <strong>Listbox</strong> to our grid and define an ItemTemplate as shown below: </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="listBox1"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">Orientation</span><span style="color: #0000ff;">="Vertical"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Author}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Description}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding ReleaseDate}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">HyperlinkButton</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Listen to this Episode"</span> <span style="color: #ff0000;">NavigateUri</span><span style="color: #0000ff;">="{Binding Link}"</span> <span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="_blank"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Let’s go ahead and add the following code to our MainPage.xaml.cs: (This will add 40,000 items to our ListBox)</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Podcast</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Description { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">public</span> DateTime ReleaseDate { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">public</span> Uri Link { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Author { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> MainPage : UserControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">public</span> List<Podcast> _samplePodcastList;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">public</span> MainPage()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> InitializeComponent();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> Loaded += <span style="color: #0000ff;">new</span> RoutedEventHandler(MainPage_Loaded);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #0000ff;">void</span> MainPage_Loaded(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span> _samplePodcastList = <span style="color: #0000ff;">new</span> List<Podcast>();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i < 40000; i++)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> Podcast PD = <span style="color: #0000ff;">new</span> Podcast();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> 28:</span> PD.Description = <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Description{0} "</span>, i.ToString());</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> 29:</span> PD.ReleaseDate = DateTime.Now.AddDays(-10000).AddHours(i);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> 30:</span> PD.Link = <span style="color: #0000ff;">new</span> Uri(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"http://www.podcast.com/{0}"</span>, i.ToString()));</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> 31:</span> PD.Author = <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"FirstName {0}. LastName {1}"</span>, i.ToString(), i.ToString());</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> 32:</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> 33:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> 34:</span> _samplePodcastList.Add(PD);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> 35:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> 36:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> 37:</span> listBox1.ItemsSource = _samplePodcastList;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> 38:</span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum39" style="color: #606060;"> 39:</span> }</pre> <!--CRLF--></div> </div> <p>At this point, if we run our application, we should get the following: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/____8_2.png"><img width="253" height="562" title="8" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="8" src="http://www.silverlightshow.net/Storage/Users/mbcrump/_____8_thumb.png" /></a></p> <p>This result, however, is not exactly what we want. It is not a <strong>WrapPanel</strong> and it is very slow navigating the 40,000 items bound to it. </p> <p>With those 40,000 items in mind, let’s head back into our MainPage.xaml and add the Telerik <strong>VirtualizingWrapPanel</strong> inside our ListBox’s ItemsPanel, using the following code snippet: </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:VirtualizingWrapPanel</span> <span style="color: #ff0000;">ItemWidth</span><span style="color: #0000ff;">="250"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Now, if we run the application, we will see something much better:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/9_4.png"><img width="688" height="374" title="9" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="9" src="http://www.silverlightshow.net/Storage/Users/mbcrump/9_thumb_1.png" /></a></p> <p>We now have our items in the ListBox using a <strong>WrapPanel</strong> and it is very easy to scroll hundreds of thousands of records. </p> <p>Meanwhile, our completed <strong>MainPage.xaml</strong> file should look like the following:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x:Class</span><span style="color: #0000ff;">="RadVirtualizingWrapPanelSample.MainPage"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #ff0000;">xmlns:d</span><span style="color: #0000ff;">="http://schemas.microsoft.com/expression/blend/2008"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #ff0000;">xmlns:mc</span><span style="color: #0000ff;">="http://schemas.openxmlformats.org/markup-compatibility/2006"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #ff0000;">xmlns:telerik</span><span style="color: #0000ff;">="http://schemas.telerik.com/2008/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #ff0000;">mc:Ignorable</span><span style="color: #0000ff;">="d"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #ff0000;">d:DesignHeight</span><span style="color: #0000ff;">="300"</span> <span style="color: #ff0000;">d:DesignWidth</span><span style="color: #0000ff;">="400"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="listBox1"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span> <span style="color: #ff0000;">Orientation</span><span style="color: #0000ff;">="Vertical"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Author}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Description}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="txtReleaseDate"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding ReleaseDate}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> 18:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">HyperlinkButton</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Listen to this Episode"</span> <span style="color: #ff0000;">NavigateUri</span><span style="color: #0000ff;">="{Binding Link}"</span> <span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="_blank"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> 19:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> 21:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> 22:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> 23:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ListBox.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> 25:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:VirtualizingWrapPanel</span> <span style="color: #ff0000;">ItemWidth</span><span style="color: #0000ff;">="250"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> 26:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> 27:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> 28:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ListBox</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> 29:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> 30:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p> </p> <h4>Thanks for Reading</h4> <p>We hope this article has you convinced at how easy it is to get started with Telerik’s RadVirtualizingWrapPanel. Don’t forget to check out parts one and three of <em>Telerik XAML Q3’11 Beta—A Walkthrough</em>: each discussing RadBarCode and ChartView, respectively.</p> <p><em>If you enjoyed this article, you can </em><a href="http://blogs.telerik.com/michaelcrump/posts.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-2_Michael-Crump-Telerik-blog"><em>find similar postings by XAML Evangelist, Michael Crump, on his Telerik blog</em></a><em>.</em></p> http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-2-of-3.aspx editorial@silverlightshow.net (Michael Crump ) http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-2-of-3.aspx#comments http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-2-of-3.aspx Wed, 02 Nov 2011 17:24:00 GMT Telerik XAML Q3'11 Beta - Walkthrough (Part 1 of 3) <p style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;">This is part 1 from an article on <strong>Telerik XAML Q3'11 Beta</strong>, authored by Michael Crump and Evan Hutnick. <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-2-of-3.aspx" target="_self">Part 2</a> | <a href="http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-3-RadChartingKit.aspx" target="_self">Part 3</a><br /> <br /> SilverlightShow will occasionally publish content related to 3rd party Silverlight and Windows Phone products, following interest and article topic requests received through our surveys and feedback email (editorial at silverlightshow dot net).<br />  </p> <p> </p> <h4>Telerik XAML Q3’11 Beta</h4> <p>Come mid-November Telerik will be releasing the official versions of RadBarCode, RadVirtualizingWrapPanel, and RadChartView—and a couple surprises to boot. ;-) In this 3-part article, <em>Telerik XAML Q3’11 Beta—A Walkthrough</em>, we give you a comprehensive technical look at each of these 3 new controls—make sure to check them all out! </p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More resources...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Upcoming SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/GetStarted.aspx">Get Started with Silverlight 4</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx">'Getting Ready for Microsoft Silverlight Exam 70-506' Ebook </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx"><img style="border:0px solid;" alt="Getting Ready for Microsoft Silverlight Exam 70-506: Ebook" src="http://www.silverlightshow.net/Storage/sl_exam_thumb_small.png" usemap="#rade_img_map_1291385581316" /></a></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> We also recommend that you <a href="http://www.telerik.com/account/your-products/trial-product-versions/trial-single-download.aspx?pmvid=2574&pid=571&utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-1_download-SL-beta">download the beta</a> so you can make the most of what you’re reading. <p>For an introductory overview you can <a href="http://blogs.telerik.com/blogs/posts/11-10-21/telerik-xaml-controls-q3-2011-beta-introduces-new-and-empowered-chartingkit.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-1_Beta-announcement">check out the official beta announcement on our Silverlight Team blog</a>.</p> <h4>Introducing RadBarCode</h4> <p>RadBarcode allows you to accurately track your physical products with native high-quality vector-based images that follow the most popular and widely-adopted barcode symbologies, including: </p> <p><em>Alpha-numeric Codes (variable length):</em> Code 128, Code 128A, Code 128B, Code 128C and Code 39</p> <p><em>Numeric Codes (fixed length):</em> Code EAN-13, Code EAN-8, Code UPC-A, PostNet and Code UPC-E</p> <p><a href="http://demos.telerik.com/silverlight/beta/#Barcode/Gallery/Code128">You can check out the BarCode demos here.</a></p> <h4>Getting Started with 3 Commonly Used Barcodes</h4> <p>We are going to build a sample project that uses Telerik’s RadBarCode in Silverlight 4— walking you through the use of Code 128, Code EAN-13, and UPC-A.</p> <p><strong>Code 128</strong></p> <p>Code 128 is a high-density barcode symbology that is used for alpha-numeric or numeric-only barcodes. It is also commonly used for labels in inventory and industrial applications.</p> <p>So, Open Visual Studio 2010 and select a new Silverlight Project, then, give it any name that you want. In this example, I have given it the name of <strong>RadBarCodeSample</strong>.</p> <p>We are going to need to add two references to our project in order to use any of the 9 barcode symbologies. </p> <ol> <li>Telerik.Windows.Controls.DataVisualization </li> <li>Telerik.Windows.Controls </li> </ol> <p><em>Make sure that you select 2011.3.1020.1040 as the version number to ensure that you are using the Q3 Beta release. </em></p> <p>Now, let’s go ahead and add the Telerik XAML Namespace to our <strong>MainPage.xaml</strong> file.  Simply double-click the MainPage.xaml file and add the following code snippet:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"</pre> <!--CRLF--></div> </div> <p>Once this is complete, we can now add our Barcode inside the Grid tag as shown below: </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadBarcode128</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="120"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="CODE 128"</span> <span style="color: #ff0000;">ShowChecksum</span><span style="color: #0000ff;">="False"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p>Our completed MainPage.xaml file should then look like the following:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x:Class</span><span style="color: #0000ff;">="RadBarCodeSample.MainPage"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #ff0000;">xmlns:d</span><span style="color: #0000ff;">="http://schemas.microsoft.com/expression/blend/2008"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #ff0000;">xmlns:mc</span><span style="color: #0000ff;">="http://schemas.openxmlformats.org/markup-compatibility/2006"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #ff0000;">xmlns:telerik</span><span style="color: #0000ff;">="http://schemas.telerik.com/2008/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #ff0000;">mc:Ignorable</span><span style="color: #0000ff;">="d"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #ff0000;">d:DesignHeight</span><span style="color: #0000ff;">="300"</span> <span style="color: #ff0000;">d:DesignWidth</span><span style="color: #0000ff;">="400"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> 9:</span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadBarcode128</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="120"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="CODE 128"</span> <span style="color: #ff0000;">ShowChecksum</span><span style="color: #0000ff;">="False"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Inside the designer our Barcode is now displayed as such: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image002%5B1%5D.jpg"><img width="293" height="99" title="clip_image002[1]" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="clip_image002[1]" src="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image002%5B1%5D_thumb.jpg" /></a></p> <p>If we wanted to show the Checksum then we would simply change the property <strong>ShowChecksum</strong> to True. </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadBarcode128</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="120"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="CODE 128"</span> <span style="color: #ff0000;">ShowChecksum</span><span style="color: #0000ff;">="True"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p>This will give us the barcode with a proper checksum—as highlighted in yellow below:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image004%5B1%5D.jpg"><img width="290" height="98" title="clip_image004[1]" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="clip_image004[1]" src="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image004%5B1%5D_thumb.jpg" /></a></p> <p>Nice work. </p> <p><strong>Code EAN-13</strong></p> <p>Let’s now take a look at Code EAN-13—barcodes, which are used worldwide for marking products often sold at retail point-of-sale. It is a 13-digit (12 data and 1 check) standard which is a superset of the original 12-digit Universal Product Code (UPC) system developed in the United States. </p> <p>Now, head back to the <strong>MainPage.xaml</strong> file and make sure the following namespace exists:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"</pre> <!--CRLF--></div> </div> <p>Once this is complete, we can add our Barcode inside the Grid tag as shown below: </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadBarcodeEAN13</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="120"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="425"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="590123412345"</span> <span style="color: #ff0000;">ShowChecksum</span><span style="color: #0000ff;">="True"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image006%5B1%5D.jpg"><img width="371" height="110" title="clip_image006[1]" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="clip_image006[1]" src="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image006%5B1%5D_thumb.jpg" /></a></p> <p><em>Please note that in this sample, we included the Checksum value. </em></p> <p>Pretty cool, huh?</p> <p><strong>UPC-A</strong></p> <p>Finally, check out another commonly used barcode called UPC-A—a 13-digit (12 data and 1 check) standard that is widely used in North America, and in countries including the UK, Australia, and New Zealand for tracking trade items in stores. </p> <p>Again, make sure your Telerik namespace exists in XAML, and then add the following code snippet: </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">telerik:RadBarcodeUPCA</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="120"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="425"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="590123412345"</span> <span style="color: #ff0000;">ShowChecksum</span><span style="color: #0000ff;">="True"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p>Inside the designer, our barcode now looks like the following:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image008%5B1%5D.jpg"><img width="378" height="117" title="clip_image008[1]" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="clip_image008[1]" src="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image008%5B1%5D_thumb.jpg" /></a></p> <p>And there you have it, <strong>Code 128, Code EAN-13</strong>, and <strong>UPC-A</strong>. With 6 more barcode symboligies included in the beta, we hope you’ll get the chance to try them all out and let us know what you think.</p> <h4>But wait, there’s more: you can easily switch between barcodes! </h4> <p>Telerik has made it very easy to switch between barcodes without adding additional references or namespaces. Simply hit “<strong>Ctrl-Space</strong>” inside of the Telerik reference to see a complete list of available barcodes, as shown below:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image009%5B1%5D.png"><img width="578" height="243" title="clip_image009[1]" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="clip_image009[1]" src="http://www.silverlightshow.net/Storage/Users/mbcrump/clip_image009%5B1%5D_thumb.png" /></a><strong></strong></p> <h4> </h4> <h4>Thanks for Reading</h4> <p>We hope this walkthrough has you convinced at how easy it is to get started with Telerik’s RadBarCode. Don’t forget to check out parts two and three of <em>Telerik XAML Q3’11 Beta</em>: RadVirtualizingWrapPanel and RadChartView, respectively.</p> <p><em>If you enjoyed this article, you can </em><a href="http://blogs.telerik.com/michaelcrump/posts.aspx?utm_source=silverlightshow&utm_medium=article&utm_campaign=Telerik-product-article_in-text_XAML-Beta-Walkthrough-1_Michael-Crump-Telerik-blog"><em>find similar postings by XAML Evangelist, Michael Crump, on his Telerik blog</em></a><em>.</em></p> http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-1-of-3.aspx editorial@silverlightshow.net (Michael Crump ) http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-1-of-3.aspx#comments http://www.silverlightshow.net/items/Telerik-XAML-Q3-11-Beta-A-Walkthrough-Part-1-of-3.aspx Wed, 02 Nov 2011 16:30:00 GMT Generating simple collections for Pivot Viewer <p>The Silverlight PivotViewer control is an easy way to have a fast and spectacular gallery for your website/webapp. In this article I'll show how to generate collections for the PivotViewer using Linq-to-Xml and the command line tool.</p> <p><span style="line-height: 30px; font-size: 20px;">PivotViewer Introduction</span></p> <p>The control enables filtering, sorting with fluid animations, and uses <a href="http://msdn.microsoft.com/en-us/library/cc645050(v=vs.95).aspx">Deep Zoom technology</a> to make the loading fast. To get an idea about it, check out the <a href="http://netflixpivot.cloudapp.net/">Netflix pivot example</a> and the <a href="http://www.wp7apphub.com/">Windows Phone Marketplace Visualizer (wp7apphub)</a>. For hosting you’ll only need a fileserver because simple collections are static and stored in xml and in (Deep Zoom) image files. For more detailed information visit the <a href="http://www.silverlight.net/learn/pivotviewer/">PivotViewer site on Silverlight.net</a>.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/wildfox/pivot-bar_thumb1_2.png"><img width="373" height="228" title="pivot-bar_thumb1" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;border: 0px;" alt="pivot-bar_thumb1" src="http://www.silverlightshow.net/Storage/Users/wildfox/pivot-bar_thumb1_thumb.png" /></a></p> <p><a href="http://cid-d1452a0f3483ee55.office.live.com/self.aspx/Nyilv%c3%a1nos/PivotViewerSample.zip">Download PivotViewerSample with preset collection</a></p> <p><a href="http://cid-d1452a0f3483ee55.office.live.com/self.aspx/Nyilv%c3%a1nos/BloggerToCxml.zip">Download source for BloggerToPivot command line converter</a></p> <h4>Data options</h4> <p>PivotViewer collections are stored in Collection XML (CXML) schemas and Deep Zoom images (DZC). There are three types of collections:</p> <ul> <li>Simple collection – Maximum 3,000 items, static pre-generated collection. <strong>This type will be covered in this article</strong>. </li> <li>Linked collection – Multiple inter-linked simple collections to overcome the size limit of Simple collections. Needs data that naturally flows into divisions, data sets can become very complex and difficult to handle. </li> <li>Just in time collection – Collection is generated dynamically, no size limit. Bandwidth and performance can become a problem. </li> </ul> <h3>Properties and structure</h3> <p>In the <a href="http://i3.silverlight.net/content/pivotviewer/collection-code/pivotcollection.xsd">CXML schema</a> first we must define the Facet Categories. These elements will be used to filter, sort or group the collection items. They could be type of String, LongString, Number, DateTime, or Link. For each item we can add multiple values for a Facet Category, so for example we can store tags here as well, not just unique categories.</p> <p>Next we can define the Items, each has a Name, an Image, and optional Description. Adding Facet Category values is also optional but each Category can have multiple values.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/wildfox/pivot-overview_thumb1_2.png"><img width="591" height="364" title="pivot-overview_thumb1" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;border: 0px;" alt="pivot-overview_thumb1" src="http://www.silverlightshow.net/Storage/Users/wildfox/pivot-overview_thumb1_thumb.png" /></a></p> <p>Facet Categories appear on the Filter Panel, the Item Name, Description and Facet values (where the Category has IsMetaDataVisible set to true) appear on the Info Panel.</p> <h3>Creating Simple collections programmatically</h3> <p>There are a number of <a href="http://www.silverlight.net/learn/pivotviewer/collection-tools/">tools</a> for generating collections, including an Excel plug-in, and there are solutions on <a href="http://pivotviewersp.codeplex.com/">CodePlex</a> as well. But there are situations when you don’t want to use Excel to generate collections, because it might not fit best with your existing data. Since the CXML is a standard XML file we can use Linq-to-Xml to manually generate the CXML and by using the command line tool to make it Deep Zoom enabled.</p> <p>So, we will need to download the <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=9a1bb862-d80c-4145-9320-b279a63bff91">PivotViewer control</a> itself, and the <a href="http://www.silverlight.net/learn/pivotviewer/download-command-line-tool/">command line tool</a>. The command line tool comes with a sample.cxml and sample images (in Pauthor/Docs). We can generate the Deep Zoom enabled CXML from the sample by running the following command:</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; height: 17px; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">Pauthor.exe /source cxml sample.cxml /target deepzoom output\sample-dz.cxml</pre> <!--CRLF--></div> </div> <p>The result will be another CXML (output/sample-dz.cxml) and a folder with the Deep Zoom images (output/sample-dz_deepzoom). The Pivot control comes with a sample app in which you can try out the generated collection. It’s located in Program files folder\Microsoft SDKs\Silverlight\v4.0\PivotViewer\Aug10\Source or you can use the attached source. (The sample in the SDK didn’t work for me out of the box, I guess the collection was removed as it returns a 404 error). You can try it by rewriting the “initialCollectionUri” parameter to localhost in PivotViewerSample.html and by copying the newly generated sample collection to the Web folder, next to the html.</p> <p>Here’s a little part of the sample xml:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 98.54%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 210px; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Collection</span> <span style="color: #ff0000;">xmlns:xsi</span><span style="color: #0000ff;">="http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color: #ff0000;">xmlns:xsd</span><span style="color: #0000ff;">="http://www.w3.org/2001/XMLSchema"</span> <span style="color: #ff0000;">xmlns:p</span><span style="color: #0000ff;">="http://schemas.microsoft.com/livelabs/pivot/collection/2009"</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="2009 AKC Dog Breeds"</span> <span style="color: #ff0000;">SchemaVersion</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/collection/metadata/2009"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">FacetCategories</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">FacetCategory</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Group"</span> <span style="color: #ff0000;">Type</span><span style="color: #0000ff;">="String"</span> <span style="color: #ff0000;">p:IsFilterVisible</span><span style="color: #0000ff;">="true"</span> <span style="color: #ff0000;">p:IsMetaDataVisible</span><span style="color: #0000ff;">="true"</span> <span style="color: #ff0000;">p:IsWordWheelVisible</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">FacetCategory</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Size"</span> <span style="color: #ff0000;">Type</span><span style="color: #0000ff;">="String"</span> <span style="color: #ff0000;">p:IsFilterVisible</span><span style="color: #0000ff;">="true"</span> <span style="color: #ff0000;">p:IsMetaDataVisible</span><span style="color: #0000ff;">="true"</span> <span style="color: #ff0000;">p:IsWordWheelVisible</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Extension</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortOrder</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Size"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortValue</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Tiny"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortValue</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Small"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortValue</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Medium"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortValue</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Large"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">p:SortValue</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Extra Large"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">p:SortOrder</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Extension</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">FacetCategory</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">...</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Items</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Item</span> <span style="color: #ff0000;">Id</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Affenpinscher"</span> <span style="color: #ff0000;">Img</span><span style="color: #0000ff;">="sample_images\{556274aa-0333-4a65-9b46-7676e2d0bbda}.jpg"</span> <span style="color: #ff0000;">Href</span><span style="color: #0000ff;">="http://www.akc.org/breeds/affenpinscher/"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Description</span><span style="color: #0000ff;">></span>The Affenpinscher is a Tiny dog.<span style="color: #0000ff;"></</span><span style="color: #800000;">Description</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Facets</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Facet</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Group"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">String</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Toy"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Facet</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Facet</span> <span style="color: #ff0000;">Name</span><span style="color: #0000ff;">="Size"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">String</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Tiny"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Facet</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Facets</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Item</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>So, to generate our CXML we can use Linq-to-xml. Let’s say we have items that have Tags and Creation time. First, we must define the Facet Categories, then we can add items to the xml:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 98.26%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 210px; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">XNamespace p = XNamespace.Get(<span style="color: #006080;">"http://schemas.microsoft.com/livelabs/pivot/collection/2009"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">XNamespace defns = XNamespace.Get(<span style="color: #006080;">"http://schemas.microsoft.com/collection/metadata/2009"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">XElement xml =</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Collection"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(XNamespace.Xmlns + <span style="color: #006080;">"p"</span>, p),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, <span style="color: #006080;">"test-collection"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"SchemaVersion"</span>, <span style="color: #006080;">"1.0"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"FacetCategories"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"FacetCategory"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, <span style="color: #006080;">"CreatedTime"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Type"</span>, <span style="color: #006080;">"DateTime"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(p + <span style="color: #006080;">"IsFilterVisible"</span>, <span style="color: #006080;">"true"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(p + <span style="color: #006080;">"IsMetaDataVisible"</span>, <span style="color: #006080;">"true"</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> ),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"FacetCategory"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, <span style="color: #006080;">"Tags"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Type"</span>, <span style="color: #006080;">"String"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(p + <span style="color: #006080;">"IsFilterVisible"</span>, <span style="color: #006080;">"true"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(p + <span style="color: #006080;">"IsWordWheelVisible"</span>, <span style="color: #006080;">"true"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(p + <span style="color: #006080;">"IsMetaDataVisible"</span>, <span style="color: #006080;">"true"</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> )),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Items"</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> );</pre> <!--CRLF--></div> </div> <p>In the code above we defined a collection named “test-collection”, that has two Facet Categories; CreatedTime type of DateTime with filtering enabled and info panel visibility; and Tags category type of String which also has filtering enabled and will be visible on the Info panel.</p> <p>Next we can cycle through our items for any data source, and add them. Note that you will need to save every item’s image to the local file system.</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.91%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 210px; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">var xitems = xml.Element(defns + <span style="color: #006080;">"Items"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">foreach</span> (var entry <span style="color: #0000ff;">in</span> Entries)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> var xitem = <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Item"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Id"</span>, entry.ID),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, entry.Title),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Img"</span>, Path.Combine(imgFolder, String.Format(<span style="color: #006080;">"{0}.jpg"</span>, entry.ID))),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Href"</span>, entry.EntryUrl),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Description"</span>, entry.EntryText),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Facets"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Facet"</span>, <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, <span style="color: #006080;">"CreatedTime"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"DateTime"</span>, <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Value"</span>, entry.Published))</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> ),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"Facet"</span>, <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Name"</span>, <span style="color: #006080;">"Tags"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> entry.Tags.Select(t =></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> XElement(defns + <span style="color: #006080;">"String"</span>, <span style="color: #0000ff;">new</span> XAttribute(<span style="color: #006080;">"Value"</span>, t))</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> )</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> )</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> )</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> );</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> xitems.Add(xitem); </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">xml.Save(cxmlName)</pre> <!--CRLF--></div> </div> <p>After saving the xml, we can run the PAuthor command line tool to generate a Deep Zoom enabled collection and run it in the PivotViewerSample project.</p> <h4>Creating pivot collection of a blog</h4> <p>To have a real world example, let's have a look at how to generate pivot data from a Blogger site (blogspot.com blog). In Blogger we can export the whole blog to an xml file, including all the posts. <br /> In my example the blog already contained more than 500 entries, so the exported xml was pretty big. I used the freely available <a href="http://www.microsoft.com/downloads/en/details.aspx?familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en">XML Notepad 2007</a> application to discover the xml's structure, and to help me figure out the parsing. First, I added a class called Entry that represented an item in the PivotViewer and a blog entry at the same time.</p> <p>Parsing was simple once I knew the structure. We need to take entry elements that has the first category as “post” (because settings are in entry elements too), and for the tags, we need to collect the “term” category values inside an entry: </p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 98.07%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 210px; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">var xml = XDocument.Load(<span style="color: #006080;">@"blog.xml"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">var ns = <span style="color: #006080;">"{http://www.w3.org/2005/Atom}"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">var tns = <span style="color: #006080;">"{http://www.blogger.com/atom/ns#}"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">var entries = from e <span style="color: #0000ff;">in</span> xml.Descendants().Elements(ns + <span style="color: #006080;">"entry"</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> orderby e.Element(ns + <span style="color: #006080;">"published"</span>).Value descending</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">where</span> e.Elements(ns+<span style="color: #006080;">"category"</span>).First().Attribute(<span style="color: #006080;">"term"</span>).Value == <span style="color: #006080;">"http://schemas.google.com/blogger/2008/kind#post"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> select <span style="color: #0000ff;">new</span> Entry</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> Title = e.Element(ns+<span style="color: #006080;">"title"</span>).Value,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> Published = Convert.ToDateTime(e.Element(ns+<span style="color: #006080;">"published"</span>).Value),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> PictureUrl = getPicasaUrl(e.Element(ns+<span style="color: #006080;">"content"</span>).Value),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> Tags = (from t <span style="color: #0000ff;">in</span> e.Elements(ns+<span style="color: #006080;">"category"</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> select t.Attribute(<span style="color: #006080;">"term"</span>).Value).Skip(1).ToList(),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> EntryHtml = e.Element(ns+<span style="color: #006080;">"content"</span>).Value </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> };</pre> <!--CRLF--></div> </div> <p>The most important property is PictureUrl for PivotViewer, in my case it was always on Picasa, so I took the first Picasa image url in every entry. I left out entries without pictures from the CXML generation. The last step was to download the associated image for each Entry, and write back the path:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">WebClient wc = <span style="color: #0000ff;">new</span> WebClient();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">int</span> id = 0;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">foreach</span> (var item <span style="color: #0000ff;">in</span> entries)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> (!String.IsNullOrEmpty(item.PictureUrl))</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> item.ImgPath = Path.Combine(<span style="color: #006080;">"C:\\Temp\\pivot_images"</span>, String.Format(<span style="color: #006080;">"{0}.jpg"</span>, id) ); </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> wc.DownloadFile(item.PictureUrl, item.ImgPath);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> item.ID = id;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> id++;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> <p>After this the xml was parsed, and was ready for CXML and PAuthor.</p> <p><a href="http://cid-d1452a0f3483ee55.office.live.com/self.aspx/Nyilv%c3%a1nos/BloggerToCxml.zip">You can get source from here.</a> </p> <h3>Hosting PivotViewer and Simple collections</h3> <p>Hosting the PivotViewer is easy, once you have a web host space or server, you can just copy the app and the collection there. The only problem I met hosting the collection was that the server didn’t know about the CXML and Deep Zoom file types. So I needed to add the following MIME types:</p> <ul> <li>.cxml – text/xml </li> <li>.dzi – text/xml </li> <li>.dzc – text/xml </li> </ul> <p>In case your server doesn’t know about Silverlight (I think this is very rare now), you’ll also need to register the .xap MIME type (application/x-silverlight-app).</p> <h3>Summary</h3> <p>PivotViewer is a very useful control with rich functionality, and can offer an easy way out if you need a spectacular gallery. It has also deeper functions that this article didn’t cover, like the Just in Time collections, or the customization of the control. The good news is that we don’t have to rely on the Excel plugin, we can generate collections however we like and have it integrated with our apps.</p> <h3>About the Author</h3> <p><img width="150" height="190" style="width: 100px; float: left; height: 134px; margin-right: 15px;" alt="Levente Mihaly" src="http://www.silverlightshow.net/Storage/levi.jpg" />My name is Levente Mihaly. I've entered the .NET world in 2006, and after graduating at Budapest University of Technology (Msc in Computer Science) I started working as a .NET software developer. After meeting technologies like WinForms and ASP.NET, Silverlight quickly became my favourite platform from the early Silverlight 2 beta stages. I was always interested in mobile development and now I'm very happy since with Windows Phone 7 I can easily expand my knowledge to the mobile world. Nowadays beside following Silverlight I'm focusing on WP7 in my free time. I'm also the runner-up of the 2010 Silverlight ecoContest. You can reach me on twitter (<a href="http://twitter.com/#!/leventemihaly" target="_blank">@leventemihaly</a>). <br />  </p> http://www.silverlightshow.net/items/Generating-simple-collections-for-Pivot-Viewer.aspx editorial@silverlightshow.net (Levente Mihály ) http://www.silverlightshow.net/items/Generating-simple-collections-for-Pivot-Viewer.aspx#comments http://www.silverlightshow.net/items/Generating-simple-collections-for-Pivot-Viewer.aspx Fri, 27 May 2011 00:00:00 GMT ItemsControl from scratch to your own controls <p style="text-align: justify;">The ItemsControl is a kind of strange presence when you are working in Silverlight.  As I've already said in a <a href="http://www.silverlightshow.net/items/Understanding-the-Content-Controls-in-Silverlight.aspx">previous article</a>, the ItemsControl is one of the three kind of controls you can meet in Silverlight, at the root of a big category of controls that are able to display lists of items. Differently from other base classes it works also if you use it as-is, but many important controls directly or indirectly inherit from it. ListBox, DataGrid, and many other beyond suspicion get their nature from the ItemsControl so, having a deep knowledge of it is really important if you want to improve your manufacturing of controls but also to use it properly into your applications.</p> <p style="text-align: justify;"><a href="http://www.silverlightshow.net/Storage/Sources/SLPG.Controls.Web.zip">Download the source code</a></p> <h3 style="text-align: justify;">Using the ItemsControl</h3> <p style="text-align: justify;">At the basis of the ItemsControl there is a really simple concept. Imagine to have an area where you can attach an indefinite number of children and they are displayed inside a given layout. These children may be equal, similar or also completely different from each other and the ItemsControl will take them in the order of appearance and it will put them into the chosen layout. Here is a short snippet that shows what I'm saying:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl.Items</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="First"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="20"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="20"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Third"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl.Items</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">The default layout defined for the ItemsControl is a StackPanel so the three buttons in the snippet will be displayed one above the other, separated by a rectangle. It is very common to have a vertical list of items in the page and most of the time it suffice but in other cases you can have the need to display the items in a completely different layout. The default layout can be changed easily using the ItemsPanel property and and ItemsPanelTemplate:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">toolkit:WrapPanel</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsPanelTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl.ItemsPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl.Items</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="First"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="20"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="20"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Third"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl.Items</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">In this snippet I used a WrapPanel as layout so the items will be organized like the files in a folder.  The customization of the layout is for sure one of the most beautiful features of the ItemsControls. </p> <p style="text-align: justify;">Adding elements to the Items collection is one way of using this ItemsControl but it is not the way the control is usually used. It may be useful sometimes - especially when you are building composable layouts - but the most of the times you will specify a DataTemplate and you will use the data binding to repeat a number of times the same fragment of code. People experienced in ASP.NET will find a strict analogy with the Repeater control. When you operate this way you create the DataTemplate - in place or into a resources section - and then you connect a data source to the ItemsSource property. Here is a snippet:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl</span> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding Path=Names}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsControl.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl.ItemTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ItemsControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">Mixing the power of the data binding with the simplicity of the ItemsControl give you great control on the output you want for your application and definitely there is not anything you can't do with this beautiful couple. Obviously there is cases where the price to pay to get something working is too much and the ItemsControl become a possible way but not the most convenient. This is the reason why many controls implements a specific logic like the ListBox, the ComboBox and the DataGrid, but you have always to remember that at the very base of them there is the ItemsControl working and many of the thing you can do with it are still possible with high level controls.</p> <h3 style="text-align: justify;">Getting access to generated elements</h3> <p style="text-align: justify;">It may sounds silly, but once you have populated an ItemsControl with databinding it is not so simple to get access to the elements generated by this operation. It is true for sure that in a well designed application you shouldn't have the need of accessing a generated UI element because the access to the source collection should suffice thanks to the benefits of the data binding, but as usual there is always a slight possibility you have to access the UI elements. </p> <p style="text-align: justify;">When the databinding happen the ItemsControl takes the items of the collection in the ItemsSource and for every one it generates a container. In the basic version of the ItemsControl the container is a ContentPresenter but usually every kind of control inheriting from ItemsControl can specify its own container. So the DataTemplate is paired with the container  and it generates the UI inside of it. Unfortunately if you try to access the Items property after the databinding you will find that the result is the datasource itself and not the generated elements. </p> <p style="text-align: justify;">For the purpose of working around to this problem they exist a bunch of methods exposed by the ItemContainerGenerator. Here is the methods with a brief description:</p> <div style="text-align: justify;"> <table border="1" cellspacing="0" cellpadding="2" width="600" align="center"> <tbody> <tr> <td valign="top" style="width: 200px;">ContainerFromIndex</td> <td valign="top" style="width: 400px;">Gets the ContentPresenter (or other container with inherited controls) at the specified index.</td> </tr> <tr> <td valign="top" style="width: 200px;">ContainerFromItem</td> <td valign="top" style="width: 400px;">Gets the ContentPresenter (or other container with inherited controls) that is generated by the specified item in the Data Source.</td> </tr> <tr> <td valign="top" style="width: 200px;">IndexFromContainer</td> <td valign="top" style="width: 400px;">Gets the index of the specified container </td> </tr> <tr> <td valign="top" style="width: 200px;">ItemFromContainer</td> <td valign="top" style="width: 400px;">Get the item that has generated the specified container</td> </tr> </tbody> </table> </div> <p style="text-align: justify;">Given these method and pairing them with the VisualTreeHelper you can solve almost every need you may have. As an example, if you want to access the TextBlock generated by an item in the previous snippet you have simply to write the following code and as a result you will see the name "Gaia" becoming bold</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">DependencyObject container = names.ItemContainerGenerator.ContainerFromItem(<span style="color: #006080;">"Gaia"</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">TextBlock tb = (TextBlock)VisualTreeHelper.GetChild(container, 0);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">tb.FontWeight = FontWeights.ExtraBold;</pre> <!--CRLF--></div> </div> </div> <h3 style="text-align: justify;">One step forward: creating your own ItemsControl</h3> <p style="text-align: justify;">Now that we have undisclosed the basic secrets of the ItemsControl the next question is for sure: <em>"how can I create my own custom ItemsControls"? </em>Silverlight offers a huge number of controls, most of them are specialized version of the ItemsControl but one time or the other you will have the need to implement your control as you usually do with the Control and ContentControl class. Implementing a custom ItemsControl is not so easy almost until when you do not have fully understand how it works. </p> <p style="text-align: justify;">The main problem with ItemsControl is that you not only have to provide your own template as you do with the other controls but you also have to implement a basic logic overriding a bunch of method that are called by the runtime when items are added or removed from the control, no matter if this happen with databinding or adding manually elements to the Items collection. </p> <p style="text-align: justify;">But starting from scratch, for the purpose of this article we will extend the example of the <a href="http://www.silverlightshow.net/items/Understanding-the-Content-Controls-in-Silverlight.aspx" target="_blank">article on ContentControls</a> implementing a BalloonList control that is able to show a list of Balloons like a chat. As usual the first step is to provide a template:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:BalloonList"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:BalloonList"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="{TemplateBinding Width}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="{TemplateBinding Height}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Padding</span><span style="color: #0000ff;">="{TemplateBinding Padding}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="{TemplateBinding HorizontalAlignment}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="{TemplateBinding VerticalAlignment}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="{TemplateBinding BorderThickness}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><!-- here goes the items --></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">This template is really simple. It is a border that wraps the items of the ItemsControl. Now we have also to create the class that represent the control and it have to inherits from ItemsControls:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">public class BalloonList : ItemsControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> public BalloonList()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> this.DefaultStyleKey = typeof(BalloonList);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">Until here there is not any difference from other types of controls. The next step is to specify where we want the items would be added inside the template. For this purpose it exists an ItemsPresenter class that - like the ContentPresenter - is able to work as a placeholder in the XAML with the sole difference that an ItemsControl can have only one ItemsPresenter. As far as I know you can also specify a number of presenters in the markup but only the first will get the items and the others will remain unchanged.</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:BalloonList"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:BalloonList"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="{TemplateBinding Width}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="{TemplateBinding Height}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Padding</span><span style="color: #0000ff;">="{TemplateBinding Padding}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="{TemplateBinding HorizontalAlignment}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="{TemplateBinding VerticalAlignment}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="{TemplateBinding BorderThickness}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ItemsPresenter</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">Now that the presenter is in place if you try to put the control in the markup and run the project you will see that it behaves exactly as a basic ItemsControl. Also if the control works fine it is unuseful just because we have to customize how the items are added and removed in the collection. There is a number of method that participate in this work and we will meet each one step by step.</p> <p style="text-align: justify;">The first thing is to decide what will be the container of the generated UI. In my case it will be the Balloon control. It is important the item you use is a ContentControl so you can seamless support both databinding and manual items. In case of manual add the Content property will contain the added item but in case of databinding the Content property will contain the item got from the datasource and the ContentTemplate will refer to the DataTemplate provided for the binding. Once we decided, we have to override the IsItemItsOwnContainerOverride. The purpose of the method is handling the case when someone add to the Items collection and instance of the container we choosed. In this case the method returns true and the logic must use the added container instead of creating an instance.</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">bool</span> IsItemItsOwnContainerOverride(<span style="color: #0000ff;">object</span> item)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> item <span style="color: #0000ff;">is</span> Balloon;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">After this method we have to provide the logic to create and initialize the container if it was not provider by the user. For this purpose we override the GetContainerForItemOverride method. This method has no parameters but have to return an instance of the container:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> DependencyObject GetContainerForItemOverride()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> Balloon balloon = <span style="color: #0000ff;">new</span> Balloon();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.ItemsContainerStyle != <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.Style = <span style="color: #0000ff;">this</span>.ItemsContainerStyle;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> balloon;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;"></p> <p style="text-align: justify;">In this call I also initialized the style of the container using a dependency property I've created. The ItemsContainerStyle will be assigned to each container generated (one for every item) and will give the user the ability to customize the aspect of the container itself. </p> <p style="text-align: justify;">Finally we are called to join the generated container with the data item. So we have to override the PrepareContainerForItemOverride() method. I would remember that the Balloon control has two Content property (Content and HeaderContent). In this case the runtime will automatically connect Content and ContentTemplate of the container with the item but we have to manually make the connection for HeaderContent and HeaderTemplate properties:</p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> PrepareContainerForItemOverride(DependencyObject element, <span style="color: #0000ff;">object</span> item)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">base</span>.PrepareContainerForItemOverride(element, item);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> Balloon balloon = element <span style="color: #0000ff;">as</span> Balloon;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> (!Object.ReferenceEquals(element, item) && balloon != <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.HeaderTemplate != <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.HeaderContent = item;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.HeaderTemplate = <span style="color: #0000ff;">this</span>.HeaderTemplate;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">After leaving the base implementation making its work we take an hard reference to the element ensuring it is a Balloon (so it is true we have created it but making the check is a sort of defensive programming). Then we check that the element and the item are not the same instance. In this case we have nothing to do because the item has been added to the Items collection so databinding will have no effects. </p> <p style="text-align: justify;"><img style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; display: inline; float: right;" title="Capture" alt="Capture" src="http://www.silverlightshow.net/Storage/Users/AndreaBoschin/____Capture.png" width="234" height="316" />After passed this check we also check for the presence of an HeaderTemplate and finally we put the data item in the HeaderContent property and the template in the HeaderTemplate property of the Balloon. This suffice and after this running a code with the BalloonList will magically show the items decorated by our own container. </p> <p style="text-align: justify;">The figure on the side is the output of the example I attached to this article. Please be aware that the output I show here imply you work providing an Item and Header DataTemplate and an ItemsContainerStyle that give the style to the orange Balloon. The item DataTemplate will put in place the text into the balloon and the HeaderTemplate will populate the Image and username in the header.</p> <p style="text-align: justify;"></p> <p style="text-align: justify;"></p> <p style="text-align: justify;">We have still not completed our work. The control seem to work fine but we have to give an implementation to a final method that is responsible of clearing resources once you remove an item from the control. </p> <p style="text-align: justify;">The method ClearContainerForItemOverride may sounds unuseful and at the first sight we may be tempted to spare some work and leave it void. Indeed it is very important that we give an implementation to free references that may cause memory leaks and someway break the connection we made between the data item and the container. In our case we put a reference to the item in the balloon header and content so we have to clear these references. Also if we had hooked up some events it would be important to clear also every delegate so we give to the Garbage Collector the opportunity of freeing resources that may be useful to other parts of the applications. </p> <div style="text-align: justify;"> <div style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;" id="codeSnippetWrapper"> <div style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;" id="codeSnippet"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> ClearContainerForItemOverride(DependencyObject element, <span style="color: #0000ff;">object</span> item)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">base</span>.ClearContainerForItemOverride(element, item);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> Balloon balloon = (Balloon)element;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">if</span> (!balloon.Equals(item))</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.ClearValue(Balloon.ContentProperty);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.ClearValue(Balloon.ContentTemplateProperty);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.ClearValue(Balloon.HeaderContentProperty);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> balloon.ClearValue(Balloon.HeaderTemplateProperty);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">In the code we manage both the Content and HeaderContent together with ContentTemplate and HeaderTemplate just to be double sure we have correcty broken every possible reference.</p> <h3 style="text-align: justify;">The beauty brought to the web</h3> <p style="text-align: justify;">I think there is not any doubt about the power of the ItemsControl. I meet it for the first time working with the first releases of WPF and I immediately fallen in love with its simplicity and flexibility. Used as-is or inheriting your own custom control is so easy and require very few code. I will never end to thank Silverlight to brought this beauty to the web.</p> <p style="text-align: justify;"><span style="line-height: 30px; font-size: 20px;">About the Author</span></p> <p><img width="136" height="174" style="width: 84px; float: left; height: 111px; margin-right: 15px;" alt="Andrea Boschin" src="http://www.silverlightshow.net/Storage/AndreaB.jpg" />Andrea Boschin is 41 years old from Italy and currently lives and works in Treviso, a beautiful town near Venice. He started to work in the IT relatively late after doing some various jobs like graphic designer and school teacher. Finally he started to work into the web and learned by himself to program in VB and ASP and later in C# and ASP.NET. Since the start of his work, Andrea found he likes to learn new technologies and take them into the real world. This happened with ASP.NET, the source of his first two MVP awards, and recently with Silverlight, that he started to use from the v1.0 in some real projects.<br />  </p> http://www.silverlightshow.net/items/ItemsControl-from-scratch-to-your-own-controls.aspx editorial@silverlightshow.net (Andrea Boschin ) http://www.silverlightshow.net/items/ItemsControl-from-scratch-to-your-own-controls.aspx#comments http://www.silverlightshow.net/items/ItemsControl-from-scratch-to-your-own-controls.aspx Mon, 16 May 2011 06:17:00 GMT Understanding the Content Controls in Silverlight <p>Once you get in touch with the Silverlight Templated controls it became evident that there are three kind of controls you can put in the XAML. Given that the structure of the page is a hierarchical tree - for this reason is is commonly called Visual Tree - you can expand this paradigm to understand the three types of controls. </p> <ul> <li>"Leaf" controls  are classes inherited from Control and they terminate the hierarchy because nothing can be inserted into them </li> <li>"Content" controls are inherited from the ContentControl class. These controls can host other elements inside of them. They are nodes in the hierarchy </li> <li>"Items" controls are usually inherited from ItemsControl and they can contain multiple instances of child controls so they are also a particular case of nodes with many branches starting from it. </li> </ul> <p>The boundary between one kind and the other is not always immediately evident when you use a control. As an example, if you consider the Button control at the first sight is may seems a leaf control but instead it is a Content control and thanks to this you can have button that hosts images and texts and not only a label. </p> <p>When you start creating a templated control the choice of the class from which inheriting is really important and you have to make the choice carefully because this choice can impact the effectiveness and reusability of the control. Many times the choice of inheriting from Control is good, but you have always to try to give a chance to the ContentControl, reasoning about the final structure you want to achieve. Content control can often give an additional gear to your work paying an often low price in when you first create the control layout. </p> <h3 style="text-align: justify;">Creating a ContentControl from scratch</h3> <p style="text-align: justify;"><a href="http://www.silverlightshow.net/Storage/Sources/SLPGControls.zip" target="_blank"><strong>Download the source code<br /> </strong></a>The first time you create a control, it is really hard to deal with the concept of content. It seems there is nothing you can't do inheriting from the simple Control class, exposing a bunch of properties to set the various aspect of the control. For the purpose of this article we will create a Balloon control similar to the ones it is usual to find in chat clients, containing the messages exchanged between the parts. The control is basically a rectangle with a spike and at the first sight it may seem it suffice to inherit from Control and expose a property "Text" to set the content of the baloon. The solution can work fine but the first time we have to put an emoticon into the chat the things become immediately hard. Given that the "Text" property can contain a string it is very difficult to put the image of the emoticon inside of the balloon because you have to write a sort of parser to detect the emoticon inside of the text and change them to an image. The things become much more complicated if you want to support image attachments, different fonts, text styles and so on.</p> <p style="text-align: justify;">If you make the Balloon a ContentControl you can think instead to its content as another branch of the Visual Tree you can put into a property of the control. The ContentControl class have a Content property where you can add other elements of the hierarchy because its type is "object". This way you can put almost everything inside the control and it becomes something widely usable. The first step to implement a content control is to create a class that inherits from the ContentControl class:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Balloon : ContentControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> Balloon()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">this</span>.DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(Balloon);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">For this purpose you can also use the Templated control wizard of Visual Studio then, when the wizard has done its work, you have to change the base class from Control to ContentControl just because the wizard by default creates a leaf control. </p> <p style="text-align: justify;">Now that the class is ready, it is time of create the default template. If you used the Visual Studio's wizard it already added the generic.xaml file and an empty template for your control. In other cases you have to manually create this file in the Themes folder and manually add the ControlTemplate this way:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ResourceDictionary</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:local</span><span style="color: #0000ff;">="clr-namespace:SLPG.Controls"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Balloon"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Balloon"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><!-- Add here the default appearance of your control --></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ResourceDictionary</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">Finally it is time to create a layout for the balloon.For the scope of this example I've created a simplified template. It is made of a grid of two columns. The leftmost column contains the spike, with a negative right margin, so it goes outside of its column and slightly enter the second column. In the other column I've two borders, one on top of the other. The spike is positioned in the middle of the two borders so it appear the spike breaks the border of the balloon. Here is the code:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Balloon"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Stretch"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Stretch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="0"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Polyline</span> <span style="color: #ff0000;">Points</span><span style="color: #0000ff;">="20,-20 0,0 20,-10"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="40"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="40"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="{TemplateBinding Background}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="Transparent"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Bottom"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,0,-20,0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="{TemplateBinding Background}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="{TemplateBinding BorderThickness}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Padding</span><span style="color: #0000ff;">="{TemplateBinding Padding}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="Transparent"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">FontWeight</span><span style="color: #0000ff;">="Bold"</span><span style="color: #0000ff;">></span>Here goes the title<span style="color: #0000ff;"></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #008000;"><!-- here goes the content --></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p style="text-align: justify;">The template in the snippet is not jet complete because it still miss the part of the content. The figure on the side shows the layout I created so you can understand the parts I've listed above. </p> <p style="text-align: justify;"><img width="186" height="105" title="Capture1" style="margin: 5px 20px 5px 0px; display: inline; float: left;" alt="Capture1" src="http://www.silverlightshow.net/Storage/Users/AndreaBoschin/Capture1_3.png" />I've prepared a layout that is really close to the balloon that you can find in the text messagges list but I've also add a title. Many of the properties of the control I'm developing are connected with a counterpart inside the layout. This helps the user to customize the look&feel of the balloon without changing the template.</p> <p style="text-align: justify;">For the moment I've marked the title and the content with a fixed text and a comment because now we have to understand how to connect the content that the developer puts into the new Balloon tag with the correct position inside the layout. </p> <h3 style="text-align: justify;"> </h3> <h3 style="text-align: justify;">The role of the ContentPresenter</h3> <p style="text-align: justify;">Now that the layout is almost ready and we tested it into the page, it is now the time to display the content in the correct position. For this purpose it exists a special element named ContentPresenter.  The ContentPresenter derive from FrameworkElement so it is not really a Control but it is able to take the content from a property of the control and display it in its place. You can think at the ContentPresenter like a placeholder that will be sobstituted by the content at runtime. </p> <p style="text-align: justify;">The ContentPresenter is so smart you can simply add it to the ControlTemplate and it automatically detects the content to present, but for the purpose of learning how it works I've manually connected its properties using the TemplateBinding markup extension:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #008000;"><!-- continue previous ControlTemplate --></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">FontWeight</span><span style="color: #0000ff;">="Bold"</span><span style="color: #0000ff;">></span>Here goes the title<span style="color: #0000ff;"></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="{TemplateBinding Content}"</span> <span style="color: #ff0000;">ContentTemplate</span><span style="color: #0000ff;">="{TemplateBinding ContentTemplate}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #008000;"><!-- continue previous ControlTemplate --></span></pre> <!--CRLF--></div> </div> <p>The ContentPresented can be slightly customized, arranging its alignment, changing its margins and size but it does not have any "visual" property like Background, BorderBrush and so on. From this side the presenter is completely transparent but inherits its aspect from the area where it is placed. Once the content presenter is in place we can write the content directly inside the Balloon tag and it will be displaied in the right place.</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">ct:Balloon</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="Orange"</span> <span style="color: #ff0000;">Padding</span><span style="color: #0000ff;">="5"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Center"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">MaxWidth</span><span style="color: #0000ff;">="200"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="White"</span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="3"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span> <span style="color: #ff0000;">Padding</span><span style="color: #0000ff;">="5"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">toolkit:WrapPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span><span style="color: #0000ff;">></span>Hey boys, this sounds<span style="color: #0000ff;"></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span>embarassing!<span style="color: #0000ff;"></</span><span style="color: #800000;">TextBlock</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Image</span> <span style="color: #ff0000;">Source</span><span style="color: #0000ff;">="smile.PNG"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="19"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="19"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Top"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">toolkit:WrapPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">ct:Balloon</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <h3 style="text-align: justify;">Hosting multiple contents</h3> <p style="text-align: justify;">We have not jet completed our work. It remains to connect the header of the ballon to a property to let a developer to customize it. In the simplest way we can connect it to a normal Text dependency property. We can instead doing something more. The ControlTemplate of a control can contain more than one presenter so we can expose an additional content property (of type object) with the name HeaderContent and then connect it to another ContentPresenter. First of all we have to expose the dependency property:</p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Balloon : ContentControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty HeaderContentProperty =</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> DependencyProperty.Register(</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #006080;">"HeaderContent"</span>, </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">object</span>), </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">typeof</span>(Balloon), </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">new</span> PropertyMetadata(<span style="color: #0000ff;">null</span>));</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> HeaderContent</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> get { <span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">object</span>)GetValue(HeaderContentProperty); }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> set { SetValue(HeaderContentProperty, <span style="color: #0000ff;">value</span>); }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">public</span> Balloon()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">this</span>.DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(Balloon);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> <p>The HeaderContent property must be of type object because it have to host every kind of element or also a simple text. Once the new dependency property is created you can add another ContentPresenter to the template and use TemplateBinding to connect its content to the new property. Once there is two presenters it is required the second one to have the TemplateBinding. Only one presenter in the template can not hanve the explicit binding. </p> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #008000;"><!-- continue previous ControlTemplate --></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="{TemplateBinding HeaderContent}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="{TemplateBinding Content}"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">ContentTemplate</span><span style="color: #0000ff;">="{TemplateBinding ContentTemplate}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; padding-top: 0px;border-style: none;"><span style="color: #008000;"><!-- continue previous ControlTemplate --></span></pre> <!--CRLF--></div> </div> <p style="text-align: justify;">Having the new HeaderContent property let you to specify a complex header instead of a simple text. You have only to explicitly open the <Balloon.HeaderContent> tag and start writing a layout element and its content. In the figure by the side you can see the final aspect of the control with Content and HeaderContent. <img width="194" height="129" title="Capture3" style="margin: 0px 0px 0px 10px; display: inline; float: right;" alt="Capture3" src="http://www.silverlightshow.net/Storage/Users/AndreaBoschin/Capture3_1.png" /></p> <h3 style="text-align: justify;">Final thoughts</h3> <p style="text-align: justify;">I'm always stunned every time I get in touch with the powerfulness of the Templated Controls in Silverlight and also in WPF. They connect an easy way of create reusable parts of UI with the full set of tools of the language. The content controls are a step forward in the direction of having a clean and well formed XAML and knowing them in deep is vital to improve you programming with Silverlight.</p> <p style="text-align: justify;">Happy control programming.</p> <p style="text-align: justify;"><span style="line-height: 30px; font-size: 20px;">About the Author</span></p> <p><img width="136" height="174" style="width: 84px; float: left; height: 111px; margin-right: 15px;" alt="Andrea Boschin" src="http://www.silverlightshow.net/Storage/AndreaB.jpg" />Andrea Boschin is 41 years old from Italy and currently lives and works in Treviso, a beautiful town near Venice. He started to work in the IT relatively late after doing some various jobs like graphic designer and school teacher. Finally he started to work into the web and learned by himself to program in VB and ASP and later in C# and ASP.NET. Since the start of his work, Andrea found he likes to learn new technologies and take them into the real world. This happened with ASP.NET, the source of his first two MVP awards, and recently with Silverlight, that he started to use from the v1.0 in some real projects.<br />  </p> http://www.silverlightshow.net/items/Understanding-the-Content-Controls-in-Silverlight.aspx editorial@silverlightshow.net (Andrea Boschin ) http://www.silverlightshow.net/items/Understanding-the-Content-Controls-in-Silverlight.aspx#comments http://www.silverlightshow.net/items/Understanding-the-Content-Controls-in-Silverlight.aspx Mon, 09 May 2011 11:58:00 GMT XNA for Silverlight developers: Part 10 - UI Elements and Menus <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px; text-align: center;">Are you interested in <strong>creating games for Windows Phone 7</strong> but don't have XNA experience? <br /> <a href="http://www.silverlightshow.net/video/XNA-for-WP7-Webinar.aspx">Watch the recording</a> of the recent intro webinar delivered by Peter Kuhn '<strong>XNA for Windows Phone 7</strong>'.<a href="https://www1.gotomeeting.com/register/256344145" target="_blank"></a><br /> </div> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 196px; float: right; height: 401px; margin-left: 10px; margin-right: 5px; padding-top: 5px;"> <h3>Don't miss...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/WP7-What-is-Windows-Phone-7.aspx">What is Windows Phone series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Exploring-Silverlight-XNA-integration-on-Windows-Phone-7.aspx">XNA article by Levente Mihály</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Windows-Phone-7-Part-1-Getting-Started.aspx">WP7 series by Andrea Boschin</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/ebook_xna.aspx">The XNA series is now available as a fully offline resource</a> </li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/ebook_xna.aspx"><img style="border:0px solid;" alt="XNA for Silverlight Developers Ebook" src="http://www.silverlightshow.net/storage/thumb_xna_ebook_cover_150_0.png" usemap="#rade_img_map_1291385581316" /></a></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>This article is part 10 of the series "XNA for Silverlight developers".</p> <p style="text-align: left;"><strong>This article is compatible with Windows Phone "Mango". <em><br /> Latest update: Aug 1, 2011.</em></strong></p> <p>Games inherently have a different UI and user experience concept compared to business applications. Where you scroll through endless lists of items and enter data in greyish forms on one side, you use a game pad or touch input to control a character through a thrilling fantasy world on the other side. In some situations however, you suddenly find yourself to be in need of all these boring controls of normal desktop applications for your game too. Whenever you want to create a menu or request certain data from the user for example, you need the same kind of UI in one way or another: text boxes, combo boxes, buttons. In this article, we'll evaluate the options you have in XNA, compare this to Silverlight, and take a quick look at the future.</p> <h3>The situation with Silverlight</h3> <p>Desktop Silverlight has a strong background in business applications, and its little brother on the phone has inherited a lot of the powerful features to create rich user interfaces. Not only do we have a built-in set of all kinds of controls that offer options for almost all scenarios, things like data binding, styles and templates also make it very easy to create appealing interfaces quickly without giving up a clear separation of data and views. A sophisticated event model helps us interact with these controls and react to user input easily, and additional containers and features allow us to create nice screen layouts and dynamic arrangements within minutes. When you are coming from this background, the situation in XNA will be – gently said – shocking.</p> <h3>What we have in XNA</h3> <p>Simply put, nothing of all this. In previous parts, we have talked about some of the involved differences regarding UI elements and user input in both worlds already. I very much encourage you to read <a href="http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-1-Fundamentals.aspx">Part 1</a> if you haven't done yet. I explain the difference between Silverlight's event-driven programming model and XNA's polling-oriented design in detail there. In <a href="http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-5-Input-touch-gestures.aspx">Part 5</a>, when we were talking about touch input for the first time, I also reviewed Silverlight's concept of controls and contrasted that with XNA's much more low-level approach. I won't go into further details regarding these difference in this article again. </p> <p>So, what <em>do</em> we have in XNA? The good news is that we have all the tools and possibilities to create a similar user experience at hand. The bad news of course is that we have to do all this manually, and depending on what your requirements are, this may result in a lot of effort and code up to a level where it is advisable to look for other options, like third-party libraries.</p> <h3>A look at a working sample</h3> <p>If you recall the sample we used in the last article you may remember that it already had an options menu that allowed the user to change some (bogus) parameters of the game:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/Menu_2.jpg"><img style="border:0px solid; background-image: none; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;" title="Menu" alt="Menu" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/Menu_thumb.jpg" width="216" height="404" /></a></p> <p>If you inspect the code for this menu, you will see that it actually tries to modularize the involved parts. There are base classes for the menu screen, a derived class for this options menu (all of which are inherited game screens from the last part), and a separate class for the menu entries on the screen that even works with events when the user selects one. The logic to determine whether a menu entry has been selected by the user or not is contained in the "MenuScreen" class and looks like this:</p> <div id="codeSnippetWrapper" class="csharpcode-wrapper"> <div id="codeSnippet" class="csharpcode"> <pre class="alt"><span id="lnum1" class="lnum"> 1:</span> <span class="rem">// look for any taps that occurred and select any entries that were tapped</span></pre> <!--CRLF--> <pre class="alteven"><span id="lnum2" class="lnum"> 2:</span> <span class="kwrd">foreach</span> (GestureSample gesture <span class="kwrd">in</span> input.Gestures)</pre> <!--CRLF--> <pre class="alt"><span id="lnum3" class="lnum"> 3:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum4" class="lnum"> 4:</span> <span class="kwrd">if</span> (gesture.GestureType == GestureType.Tap)</pre> <!--CRLF--> <pre class="alt"><span id="lnum5" class="lnum"> 5:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum6" class="lnum"> 6:</span> <span class="rem">// convert the position to a Point that we can test against a Rectangle</span></pre> <!--CRLF--> <pre class="alt"><span id="lnum7" class="lnum"> 7:</span> Point tapLocation = <span class="kwrd">new</span> Point((<span class="kwrd">int</span>)gesture.Position.X, (<span class="kwrd">int</span>)gesture.Position.Y);</pre> <!--CRLF--> <pre class="alteven"><span id="lnum8" class="lnum"> 8:</span>  </pre> <!--CRLF--> <pre class="alt"><span id="lnum9" class="lnum"> 9:</span> <span class="rem">// iterate the entries to see if any were tapped</span></pre> <!--CRLF--> <pre class="alteven"><span id="lnum10" class="lnum"> 10:</span> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i < menuEntries.Count; i++)</pre> <!--CRLF--> <pre class="alt"><span id="lnum11" class="lnum"> 11:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum12" class="lnum"> 12:</span> MenuEntry menuEntry = menuEntries[i];</pre> <!--CRLF--> <pre class="alt"><span id="lnum13" class="lnum"> 13:</span>  </pre> <!--CRLF--> <pre class="alteven"><span id="lnum14" class="lnum"> 14:</span> <span class="kwrd">if</span> (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))</pre> <!--CRLF--> <pre class="alt"><span id="lnum15" class="lnum"> 15:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum16" class="lnum"> 16:</span> <span class="rem">// select the entry</span></pre> <!--CRLF--> <pre class="alt"><span id="lnum17" class="lnum"> 17:</span> OnSelectEntry(i, PlayerIndex.One);</pre> <!--CRLF--> <pre class="alteven"><span id="lnum18" class="lnum"> 18:</span> }</pre> <!--CRLF--> <pre class="alt"><span id="lnum19" class="lnum"> 19:</span> }</pre> <!--CRLF--> <pre class="alteven"><span id="lnum20" class="lnum"> 20:</span> }</pre> <!--CRLF--> <pre class="alt"><span id="lnum21" class="lnum"> 21:</span> }</pre> <!--CRLF--></div> </div> <p>The approach used here is that the input is handled on the screen level. The screen iterates over all controls and uses the available information about the current user input and control dimensions to decide whether a menu entry is affected by the user input or not. Here is a flow chart of how the logic works:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/____image_8.png"><img style="border:0px solid; background-image: none; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;" title="image" alt="image" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/____image_thumb_3.png" width="248" height="702" /></a></p> <p>What the sample implementation does when a 'click' is detected is tell the menu entry to raise it's "Selected" event. This may seem a bit convoluted at first, but it actually enables derived classes (like the options menu) to do something meaningful with this event. </p> <p>A different approach would be to not have any logic in the screen class itself, but simply pass through the request to handle user input to each one of the controls on the screen. Then every menu entry could decide itself what to do with the input, which would be more flexible. Not matter what alternative or variation you use, this is basically how you would create the core logic for any custom UI concept: Since there is no built-in concept of UI elements in XNA, let alone a mechanism to inform you a particular one has been selected, it really comes down to creating your own elements, and then simply iterating over all of them to see if one is affected by the current user input.</p> <p>Of course all the menu entries in the example only cycle through a given set of values (including an on/off switch) or implement only a simple increment logic. But based on this rather simple structure you could already build a set of controls that is able to handle a lot of the most common requirements. The "MenuEntry" class is designed very openly for this. By overriding the "Update" and "Draw" methods you are able to change and extend the logic as well as the visual appearance of the entries.</p> <h4>Text input</h4> <p>Things are a bit different for text input you need to request from the user. While it would be possible to design and display a textbox-like control, the problem with this is the input method on the phone device. Apart from few exceptions with hardware keyboards, Windows Phone 7 relies on a SIP (<a href="http://en.wikipedia.org/wiki/Soft_Input_Panel">Soft Input Panel</a>). At the moment, there is no way to bring up the SIP to interact with your game, which means you would have to create your own on-screen keyboard. This is something far from being trivial (think different cultures etc.) and would most likely result in a user experience different from the rest of the phone. It also doesn't scale once additional languages are supported by the system and therefore needs adjustments once updates for this are rolled out by Microsoft.</p> <p>The solution to this once again is the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamerservices.guide.aspx">Guide class</a>. It has a method "<a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamerservices.guide.beginshowkeyboardinput.aspx">BeginShowKeyboardInput</a>" that brings up a simple data input screen with a customizable title, description and a text box. It handles all the SIP logic for you, and once the user has finished entering their data, a callback will be invoked to notify you about the result, which you then can use for whatever you want.</p> <p>This limitation has been heavily discussed in the community. It means that you cannot have a consistent design for the data input screen and your game (you cannot change its appearance), and you also don't have any direct control over the functionality (e.g. you cannot restrict input to certain characters or do other sorts of validation on that screen etc.). We'll see how this will be changed in the future below.</p> <p>As an example I've extended the sample from the last article and added text input to it. For this, I simply switch to the high scores list when the user taps the game play screen to simulate a successfully finished game. In this case (coming from the game play instead of the main menu) the user is prompted for their name to create an entry in the high scores list:</p> <div id="codeSnippetWrapper" class="csharpcode-wrapper"> <div id="codeSnippet" class="csharpcode"> <pre class="alt"><span id="lnum1" class="lnum"> 1:</span> <span class="rem">// is the user supposed to enter their name?</span></pre> <!--CRLF--> <pre class="alteven"><span id="lnum2" class="lnum"> 2:</span> <span class="kwrd">if</span> (EnterHighscore)</pre> <!--CRLF--> <pre class="alt"><span id="lnum3" class="lnum"> 3:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum4" class="lnum"> 4:</span> <span class="rem">// start showing the input screen</span></pre> <!--CRLF--> <pre class="alt"><span id="lnum5" class="lnum"> 5:</span> Guide.BeginShowKeyboardInput(ControllingPlayer.Value,</pre> <!--CRLF--> <pre class="alteven"><span id="lnum6" class="lnum"> 6:</span> <span class="str">"High Score!"</span>,</pre> <!--CRLF--> <pre class="alt"><span id="lnum7" class="lnum"> 7:</span> <span class="str">"Please enter your name"</span>,</pre> <!--CRLF--> <pre class="alteven"><span id="lnum8" class="lnum"> 8:</span> <span class="kwrd">string</span>.Empty,</pre> <!--CRLF--> <pre class="alt"><span id="lnum9" class="lnum"> 9:</span> KeyboardInputFinished,</pre> <!--CRLF--> <pre class="alteven"><span id="lnum10" class="lnum"> 10:</span> <span class="kwrd">null</span>,</pre> <!--CRLF--> <pre class="alt"><span id="lnum11" class="lnum"> 11:</span> <span class="kwrd">false</span>);</pre> <!--CRLF--> <pre class="alteven"><span id="lnum12" class="lnum"> 12:</span>  </pre> <!--CRLF--> <pre class="alt"><span id="lnum13" class="lnum"> 13:</span> <span class="rem">// set the flags we need</span></pre> <!--CRLF--> <pre class="alteven"><span id="lnum14" class="lnum"> 14:</span> EnterHighscore = <span class="kwrd">false</span>;</pre> <!--CRLF--> <pre class="alt"><span id="lnum15" class="lnum"> 15:</span> _userInputInProgress = <span class="kwrd">true</span>;</pre> <!--CRLF--> <pre class="alteven"><span id="lnum16" class="lnum"> 16:</span> <span class="kwrd">return</span>;</pre> <!--CRLF--> <pre class="alt"><span id="lnum17" class="lnum"> 17:</span> }</pre> <!--CRLF--></div> </div> <p>The most important argument of the method is the "KeyboardInputFinished" callback, a method which is invoked automatically when the user closes the input screen. The second flag is used to pause our custom update and draw logic as long as the input screen is visible. This is necessary because the keyboard input screen works asynchronously, which means that the game continues to run in the background. The callback implementation looks like this:</p> <div id="codeSnippetWrapper" class="csharpcode-wrapper"> <div id="codeSnippet" class="csharpcode"> <pre class="alt"><span id="lnum1" class="lnum"> 1:</span> <span class="kwrd">private</span> <span class="kwrd">void</span> KeyboardInputFinished(IAsyncResult ar)</pre> <!--CRLF--> <pre class="alteven"><span id="lnum2" class="lnum"> 2:</span> {</pre> <!--CRLF--> <pre class="alt"><span id="lnum3" class="lnum"> 3:</span> <span class="rem">// reset flag</span></pre> <!--CRLF--> <pre class="alteven"><span id="lnum4" class="lnum"> 4:</span> _userInputInProgress = <span class="kwrd">false</span>;</pre> <!--CRLF--> <pre class="alt"><span id="lnum5" class="lnum"> 5:</span>  </pre> <!--CRLF--> <pre class="alteven"><span id="lnum6" class="lnum"> 6:</span> <span class="rem">// get the result</span></pre> <!--CRLF--> <pre class="alt"><span id="lnum7" class="lnum"> 7:</span> <span class="kwrd">string</span> result = Guide.EndShowKeyboardInput(ar);</pre> <!--CRLF--> <pre class="alteven"><span id="lnum8" class="lnum"> 8:</span> <span class="kwrd">if</span> (!<span class="kwrd">string</span>.IsNullOrEmpty(result))</pre> <!--CRLF--> <pre class="alt"><span id="lnum9" class="lnum"> 9:</span> {</pre> <!--CRLF--> <pre class="alteven"><span id="lnum10" class="lnum"> 10:</span> <span class="rem">// add a bogus highscore entry</span></pre> <!--CRLF--> <pre class="alt"><span id="lnum11" class="lnum"> 11:</span> <span class="kwrd">string</span> entry = <span class="kwrd">string</span>.Format(<span class="str">"11. {0} - {1}"</span>, result, 10);</pre> <!--CRLF--> <pre class="alteven"><span id="lnum12" class="lnum"> 12:</span> _highScores.Add(entry);</pre> <!--CRLF--> <pre class="alt"><span id="lnum13" class="lnum"> 13:</span> }</pre> <!--CRLF--> <pre class="alteven"><span id="lnum14" class="lnum"> 14:</span> }</pre> <!--CRLF--></div> </div> <p>As you can see, by invoking the "<a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamerservices.guide.endshowkeyboardinput.aspx">EndShowKeyboardInput</a>" method on the Guide class you will receive the string the user has input. In our case this string is only used to create a bogus additional high score entry, but you should get the idea how you can use this mechanism to query input from the user.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/Highscores_2.jpg"><img style="border:0px solid; background-image: none; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;" title="Highscores" alt="Highscores" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/Highscores_thumb.jpg" width="216" height="403" /></a></p> <h3>Further resources</h3> <p>Apart from the game state sample that we've used here, Microsoft has also published another example in the App Hub Education section that covers UI elements and menus in XNA. It is named "User Interface Controls" and can be found <a href="http://create.msdn.com/en-US/education/catalog/sample/ui_controls">here</a>. This example contains a set of base classes for the control itself as well as a panel. Derived from this it has an image control, a page flip control to flick through items and scrolling controls to display lists of items. Even if you don't use the code as-is, it should also be possible to get some ideas from the samples to develop your own controls.</p> <p>In the past there have also been some attempts to create UI frameworks for XNA, some of which looked very promising. Unfortunately a lot of them were discontinued after a while, a few even disappeared completely. One that is still actively developed (and brings a lot more than just UI elements) is the Nuclex Framework which can be found <a href="http://nuclexframework.codeplex.com/wikipage?title=Nuclex.UserInterface&referringTitle=Documentation">on Codeplex</a>.</p> <p>Another interesting framework is <a href="http://red-badger.com/Blog/post/Introducing-XPF-e28093-A-Layout-Framework-for-XNA.aspx">XPF</a>, which has some strong layout features. It has not yet reached production level quality, but it has nightly debug builds you can use for testing. Apparently <a href="http://red-badger.com/Blog/post/XPF-Roadmap-and-Licensing.aspx">the licensing</a> will include a free version for non-commercial and open source development.</p> <h3>Integrating XNA and Silverlight</h3> <p>An obvious solution to all of XNA's lacking UI features on Windows Phone 7 would be to allow mixing Silverlight with XNA graphics in the same application, so a game can make use of Silverlight's powerful features too. In fact, some people started to investigate this very early on, but of course application certification requirements of the RTM version explicitly forbid doing this, so any success with it was futile. </p> <p>Things very much change with Windows Phone "Mango", where you have the chance and are allowed to do exactly that. The general application structure is significantly different when you make use of this, because then Silverlight needs to take the leading role, and using XNA is achieved by switching to it from Silverlight using a feature named "sharing mode". I discuss and demonstrate all the technical details in <a href="http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-12-Mango-1.aspx">part 12</a> of this series, which of course also comes with full source code for you to explore this.</p> <p>The important thing is that you can decide what parts of your game you want to implement with Silverlight, and what parts should be handled by XNA. You can use all of Silverlight's UI features to create and design your menus and other data-centric screens like highscore lists, and then switch to XNA for the actual gameplay and game content rendering. The following diagram shows a logical screen structure for such a setup, with each rectangle representing a phone application page:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__________image_2.png"><img style="border:0px; background-image: none; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;" title="image" alt="image" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/___________image_thumb.png" width="419" height="290" /></a></p> <p>But the good news doesn't stop here. You are even able to mix Silverlight and XNA content on the <em>same</em> page if you want. This allows you to e.g. use the more sophisticated Silverlight text rendering directly on your XNA gameplay screen. HUD overlays or other things can be handled with default Silverlight UI elements more easily, and then used in your XNA rendering process with the help of the new <a href="http://msdn.microsoft.com/en-us/library/hh221570(v=XNAGameStudio.41).aspx">UIElementRenderer class</a>. </p> <p>The beauty of this approach is that Silverlight elements are not forced into a passive role by this. User input can be routed to these elements, and with a little bit of work you can even use elements like text boxes on XNA-controlled pages, and have the user enter data using the SIP. And of course all the dynamic goodies of Silverlight, for example it's excellent animation features, can be used in this scenario too.</p> <p>Again, <a href="http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-12-Mango-1.aspx">part 12</a> of this series shows how to do most of this by demonstrating the UIElementRenderer in combination with Silverlight animations on an application page that is rendered by XNA.</p> <h3>Summary</h3> <p>In this article we once again learned that the more low-level nature of XNA (which has a lot of advantages in game programming) comes at the cost of a lot less comfort. Especially when it comes to creating user interfaces like menus things can easily become complex and result in a lot of work, to a point where it absolutely makes more sense to use some already available commercial or free third-party libraries. However, we've also seen that the first major update to the platform ("Mango") brings exciting news about the future of XNA and Silverlight integration, which will make a lot of these things much easier. If you are interested in the updated sample code that contains the user text input code, you can get it here:</p> <p><a href="http://www.silverlightshow.net/Storage/Sources/XNA_for_Silverlight_developers_Part10.zip" target="_blank">Download source code</a></p> <p>As always, feel free to add your comments below or provide feedback to me directly.</p> <h3>About the author</h3> <p>Peter Kuhn aka "Mister Goodcat" has been working in the IT industry for more than ten years. After being a project lead and technical director for several years, developing database and device controlling applications in the field of medical software and bioinformatics as well as for knowledge management solutions, he founded his own business in 2010. Starting in 2001 he jumped onto the .NET wagon very early, and has also used Silverlight for business application development since version 2. Today he uses Silverlight, .NET and ASP.NET as his preferred development platforms and offers training and consulting around these technologies and general software design and development process questions.</p> <p>He created his first game at the age of 11 on the Commodore 64 of his father and has finished several hobby and also commercial game projects since then. His last commercial project was the development of Painkiller:Resurrection, a game published in late 2009, which he supervised and contributed to as technical director in his free time. You can find his tech blog here: <a href="http://www.pitorque.de/MisterGoodcat/">http://www.pitorque.de/MisterGoodcat/</a></p> http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-10-UI-Elements-and-Menus.aspx editorial@silverlightshow.net (Peter Kuhn ) http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-10-UI-Elements-and-Menus.aspx#comments http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-10-UI-Elements-and-Menus.aspx Wed, 27 Apr 2011 00:00:00 GMT ClassifiedCabinet: A Quick Start <p><span style="line-height: 30px; font-size: 20px;">Introduction</span></p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>Don't miss...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Free SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Getting-ready-for-the-exams-Part-1.aspx">Getting ready for Silverlight Exam 70-506 series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Simulating-rain-in-Silverlight.aspx">Simulating rain in Silverlight </a></li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx">Pro Business Apps with SL4 book: </a></li> </ul> <p style="padding-bottom: 5px;">        <a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx"> <img alt="Pro Business Applications with Silverlight 4 book" src="http://www.silverlightshow.net/Storage/ProSL4.jpg" /></a> </p> <p style="font-size: 12px;">           <a href="http://www.silverlightshow.net/Books.aspx">Show more books</a><img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>The aim of this article is to serve as a short introduction to the new <a href="http://classifiedcabinet.codeplex.com/">ClassifiedCabinet</a> control published on <a href="http://www.codeplex.com/">codeplex</a>. I tried to give you everything that is necessary to start using the control, without going into too much detail. I’ll be building a very simple project from scratch that should demonstrate the basic usage of the control. Of course, the source code of the sample is available for download. By the way here is a live demo of what we want to achieve:</p> <p><a href="http://www.silverlightshow.net/Storage/demos/ClassifiedCabinetDemo/ClassifiedCabinetDemo.html" target="_blank">View sample live demo</a></p> <p><a href="http://www.silverlightshow.net/Storage/Sources/ClassifiedCabinet_Demo.zip" target="_blank">Download sample source code</a></p> <h3>Set Up the Project</h3> <p>OK, so let’s get started. First we need to create a new Silverlight project. I’ve called it “ClassifiedCabinetDemo”. Then you need to add a reference to the <strong>CompletIT.Windows.Controls.ClassifiedCabinet.dll</strong>. To keep things as simple as possible, I just copied the file to the “Debug” folder of the demo project and added the reference from there. Now you can just go to the MainPage.xaml, add the CompletIT.Windows.Controls namespace from the CompletIT.Windows.Controls.ClassifiedCabinet assembly and start using the control right away.</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x:Class</span><span style="color: #0000ff;">="ClassifiedCabinetDemo.MainPage"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:d</span><span style="color: #0000ff;">="http://schemas.microsoft.com/expression/blend/2008"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:mc</span><span style="color: #0000ff;">="http://schemas.openxmlformats.org/markup-compatibility/2006"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">xmlns:completITControls</span><span style="color: #0000ff;">="clr-namespace:CompletIT.Windows.Controls;assembly=CompletIT.Windows.Controls.ClassifiedCabinet"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">mc:Ignorable</span><span style="color: #0000ff;">="d"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">d:DesignHeight</span><span style="color: #0000ff;">="600"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">d:DesignWidth</span><span style="color: #0000ff;">="900"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="LayoutRoot"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="White"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">completITControls:ClassifiedCabinet</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="900"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="600"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">UserControl</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>This is the code of the MainPage.xaml so far. I’ve added the xml namespace and the ClassifiedCabinet control itself. However, this is what it looks like:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/FirstLook_2.png"><img width="300" height="225" title="First Look at the ClassifiedCabinet" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="First Look at the ClassifiedCabinet" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/FirstLook_thumb.png" /></a></p> <p>It seems we’ll need some extra work to make it do something useful, since right now the control is just notifying the user that there is nothing to display. It’s not very impressive, isn’t it? Well, let’s give it some actual data to display and see what happens.</p> <h3>Adding the Categories</h3> <p>I have created an object to hold all the data we need and set it as the page’s data context, so we can just bind the necessary properties. First you need to set the <strong>CategoriesItemsSource</strong>. As its name suggests, it is a collection containing the categories. After giving the the ClassifiedCabinet control its categories, you’ll need to tell it how to display them. Basically, you have the standard means to do so – you have an option to set the path to a member to display using <strong>CategoriesDisplayMemberPath</strong> or if you need something more complicated you can change the whole DataTemplate using the <strong>CategoriesItemTemplate</strong> property. And just a quick note - as it is the case with other such controls, you can’t use both at the same time as it makes no sense, so just choose whatever seems more appropriate. There are other properties that allow you to fine-tune the look-and-feel of the categories section, but I shall look at these in some of the next articles, so we can keep this quick start really quick. For now it is enough to say that they’re all prefixed with “Categories” and have pretty intuitive names, so if you want you can go ahead and play with the control a little to discover them. Actually, we are going to use one more property now – <strong>CategoriesWidth</strong>, but I think you can all figure out what it does. Let’s see what we have got so far:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">completITControls:ClassifiedCabinet</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="900"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="600"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesItemsSource</span><span style="color: #0000ff;">="{Binding Categories}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesDisplayMemberPath</span><span style="color: #0000ff;">="Name"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesWidth</span><span style="color: #0000ff;">="250"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p>As you can see the main page’s data context has a property called Categories which is basically a collection of Category objects. Right now every category has just a name and that’s what we are using to display it. Let’s see what we have done:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/Categories_2.png"><img width="300" height="200" title="A Nice Categories Section" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="A Nice Categories Section" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/Categories_thumb.png" /></a></p> <p>The categories section is ready and it looks kind of nice, doesn’t it? However, the items section is still empty – no shelves yet. OK, let’s see what we can do about it.</p> <h3>Adding the Items</h3> <p>Well, no surprises here. We have an <strong>ItemsSource</strong> property that we can use to give the ClassifiedCabinet its items. Of course, we’ll need to define an appropriate data template to tell it, how we want it to visualize them, so there is an <strong>ItemTemplate </strong>property as well. I have bound the <strong>ItemsSource</strong> property to a collection of Book objects in my data context. The Book object is very simple – it has just a Title, Author, PublishDate and Description properties that are all of type string for simplicity. My DataTemplate for this book is just a border and a TextBlock with the book’s title – nothing fancy. Here it is:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="ClassifiedCabinetItemTemplate"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="DeepSkyBlue"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="DodgerBlue"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="3"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="150"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="200"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Title}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">TextAlignment</span><span style="color: #0000ff;">="Center"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontFamily</span><span style="color: #0000ff;">="Verdana"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="White"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="14"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="10"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>And that’s all. You can now run the project and you’ll have all the shelves populated and the ClassifiedCabinet has even added appropriate labels to every shelf. You can also zoom and move around with the mouse – isn’t it cool? </p> <p><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/Items_2.png"><img width="300" height="200" title="Items Are Added to the Cabinet" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="Items Are Added to the Cabinet" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/Items_thumb.png" /></a></p> <p>However, we are not done, yet. There are few more things we need to take care of. You may have noticed that the books are not associated with particular categories, but instead the cabinet seems to have placed all the books on every shelf. Well, that is perfectly normal, since we haven’t yet told it how we want it to “classify” them. However, we’ll do this in the next section, while now we’ll look at something else that appears to be missing. If you click on an item in the cabinet, you get a fancy dialog that contains …nothing. Luckily, there is a property for that. It’s called <strong>ItemDetailsTemplate</strong> and it is another DataTemplate for the book that’s expected to give more details about it and is used to present the item in the dialog. You may have noticed that the book data class has four properties, but we are using only one of them. Well, now we’ll be adding the rest three. So the idea is that the DataContext for both DataTemplates is the same – an object from the collection you bind the cabinet’s ItemsSource to (in our case a Book), but you define two data templates – one to be used while the item is on the shelf and another, when the user clicked on the item and opened the dialog to see something more about a particular item. By the way, while the author and publish date are correct, the description of the book is always the same lorem-ipsum – I didn’t want to have too much sample data in the project. Finally, here’s our template for the dialog:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">DataTemplate</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="ClassifiedCabinetItemDetailsTemplate"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="15"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.RowDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RowDefinition</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.RowDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Row</span><span style="color: #0000ff;">="0"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="18"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Title}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Row</span><span style="color: #0000ff;">="1"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="12"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontStyle</span><span style="color: #0000ff;">="Italic"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Author, StringFormat='By \{0}'}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Row</span><span style="color: #0000ff;">="2"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="12"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontStyle</span><span style="color: #0000ff;">="Italic"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding PublishDate, StringFormat='Released on \{0}'}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Row</span><span style="color: #0000ff;">="3"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FontSize</span><span style="color: #0000ff;">="12"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="15"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">TextWrapping</span><span style="color: #0000ff;">="Wrap"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{Binding Description}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Here’s how it looks in action:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/ItemDetails_2.png"><img width="300" height="200" title="The Details Window" style="border:0px solid;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="The Details Window" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/ItemDetails_thumb.png" /></a></p> <p>OK, so that’s all you need to add the items to the cabinet. However, they are not classified as we have already mentioned. We wanted different items in the different categories, right? Let’s see how it is done.</p> <h3>Classifying the Items</h3> <p>Basically, it’s really easy - you only have to set a property called <strong>ClassifierFunc</strong>, which is a function that controls the whole process. The property is of type <strong>Func</strong><object,><strong><object, IEnumerable, IEnumerable></strong>, so this is a function that takes an object and an enumeration as arguments and returns another enumeration. In fact, here’s the signature of the function to make it more clear:</object,></p> <div id="codeSnippetWrapper"> <div id="codeSnippetWrapper"> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 39px; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> IEnumerable Classify(<span style="color: #0000ff;">object</span> category, IEnumerable items);</pre> <!--CRLF--></div> </div> <div>Now the ClassifiedCabinet will call the function every time you change its ItemsSource or CategoriesItemsSource changes, to classify the items. The first argument is the category object and you can safely cast it to the type of your category. The second is an enumeration containing the items that have to be classified. Note that it doesn’t necessary contain all the items. The function has to return another enumeration that contains just these items that belong to the specified category. In a sense, it filters the items according to provided category. Here’s an example:</div> <div> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> IEnumerable Classify( <span style="color: #0000ff;">object</span> category, IEnumerable books )</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> books.Cast<Book>().Where( book => book.Category == category );</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> </div> <p>It’s quite simple, isn’t? I added a property Category to the Book class and I just return the books that say they belong to the category in question. In reality, you may want to associate a book with more than one category. This is easy as well, but for the sake of simplicity we’ll stick to the one-category version in this example. Having the function defined like this, now you need a property that wraps the function for use in binding. The type of the property, of course has to be <strong>Func</strong><object,><strong><object, IEnumerable, IEnumerable></strong>. Once you bind the <strong>ClassifierFunc</strong> property of the ClassifiedCabinet to the newly defined property on the data context, you have it all set up.</object,></p> <div><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/ClassifiedItems_5.png"><img width="300" height="200" title="The Items are Classified" style="border:0px;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="The Items are Classified" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/ClassifiedItems_thumb_1.png" /></a></div> <div> </div> <p>So what’s left? Well, not much actually, we are almost done. The only thing that still doesn’t work is the filter that’s located on top of the categories.</p> <h3>Filtering the items</h3> <p>The filtering is actually quite similar to the classifying. In fact, the only difference is that the filtering function receives the current input from the user instead of the category as a first argument and should return the items that pass this filter according to its custom logic. In our case we want to return all books that contain the user input, either in the title or in the author fields. We are performing a case-insensitive search. Here’s the code of the function, along with its property wrapper required for data-binding:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">public</span> Func<<span style="color: #0000ff;">string</span>, IEnumerable, IEnumerable> FilterFunc</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> get</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.Filter;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;">private</span> IEnumerable Filter( <span style="color: #0000ff;">string</span> userInput, IEnumerable books )</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">string</span> uppercaseUserInput = userInput.ToUpper( CultureInfo.CurrentUICulture );</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;">return</span> books.Cast<Book>().Where( book =></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> book.Title.ToUpper( CultureInfo.CurrentUICulture ).Contains( uppercaseUserInput ) ||</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> book.Author.ToUpper( CultureInfo.CurrentUICulture ).Contains( uppercaseUserInput ) );</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;">}</pre> <!--CRLF--></div> </div> <p>After you bind the actual property of the control, you can run the project and test the filtering functionality of the ClassifiedCabinet. For example I have typed ‘game’ and here’s what I got:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/GStoyanov/Filtering_2.png"><img width="300" height="200" title="Filtering" style="border:0px;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="Filtering" src="http://www.silverlightshow.net/Storage/Users/GStoyanov/Filtering_thumb.png" /></a></p> <p>As you can see there are only three books on this topic right now. Also the shelves that don’t have any books related to the search have disappeared and their respective categories in the categories menu are now disabled. Just in case, let’s add post the final version of the xaml:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">completITControls:ClassifiedCabinet</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="900"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="600"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesItemsSource</span><span style="color: #0000ff;">="{Binding Categories}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesDisplayMemberPath</span><span style="color: #0000ff;">="Name"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">CategoriesWidth</span><span style="color: #0000ff;">="250"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">ItemsSource</span><span style="color: #0000ff;">="{Binding Items}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">ItemTemplate</span><span style="color: #0000ff;">="{StaticResource ClassifiedCabinetItemTemplate}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">ItemDetailsTemplate</span><span style="color: #0000ff;">="{StaticResource ClassifiedCabinetItemDetailsTemplate}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">ClassifierFunc</span><span style="color: #0000ff;">="{Binding ClassifierFunc}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <span style="color: #ff0000;">FilterFunc</span><span style="color: #0000ff;">="{Binding FilterFunc}"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <h3>Conclusion</h3> <p>Well, that is all you need to start using the ClassifiedCabinet control. As you can imagine, it’s quite a huge control that has a lot of options, supports a lot of different scenarios and with little imagination can be used in a lot of different situations.</p> <p>Since this was intended as a short introductory article, there is a lot of things left and I’ll be writing a few more articles to cover some of the more advanced usages of the classified cabinet, but I hope that this’ll be enough to get you all started. </p> <p>Also I hope you like the control. I think it’s really cool and can do a lot of things, but I ‘m really looking forward to getting some actual feedback and I’ll be interested to know what you think about it.</p> <div></div> </div> <div></div> </div> http://www.silverlightshow.net/items/ClassifiedCabinet-A-Quick-Start.aspx editorial@silverlightshow.net (Georgi Stoyanov ) http://www.silverlightshow.net/items/ClassifiedCabinet-A-Quick-Start.aspx#comments http://www.silverlightshow.net/items/ClassifiedCabinet-A-Quick-Start.aspx Wed, 02 Mar 2011 00:00:00 GMT Using the AutoCompleteBox <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; margin-left: 10px; padding-top: 5px;"> <div style="text-align: center;"><strong>This article is also available in print (Word, PDF) and e-reader formats (MOBI, EPUB).</strong></div> <div style="text-align: center;"> <div style="text-align: center;"><strong><strong>Download all formats, including source code for</strong><span style="font-weight: normal;"> <strong>$0.99.</strong> </span> <a href="https://www.e-junkie.com/ecom/gb.php?c=cart&i=1012207&cl=173172&ejc=2" class="ec_ejc_thkbx" style="font-weight: normal;" onclick="javascript:return EJEJC_lc(this);" target="ej_ejc"><img style="border:0px solid; margin-top: 2px;" alt="Add to Cart" src="http://www.e-junkie.com/ej/ej_add_to_cart.gif" /></a></strong></div> </div> </div> <p><strong><em>This article is compatible with the latest version of Silverlight.</em></strong></p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>Don't miss...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-1-Fundamentals.aspx" shape="rect">Peter Kuhn's XNA series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Free SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx">Pro Business Applications with SL4 book: </a></li> </ul> <p style="padding-bottom: 5px;">        <a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx"> <img alt="Pro Business Applications with Silverlight 4 book" src="http://www.silverlightshow.net/Storage/ProSL4.jpg" /></a> </p> <p style="font-size: 12px;">           <a href="http://www.silverlightshow.net/Books.aspx" shape="rect">Show more books</a><img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>The auto-complete box is a mix of a normal text box that allows the user to type in arbitrary text, and the drop-down list feature of a combo box that provides a fixed set of entries the user can select from. You're most likely using a control like this every day; for example, search engines like Bing and other web sites use these controls to provide a list of suggestions you can pick from without having to finish typing the full term, or they even provide a list of terms you entered previously to make repeated entries more comfortable for you.</p> <p>The fact that the list of entries is filtered on the fly as the user types into the text box makes picking an entry from a large list possible very quickly. Even if users only remember parts of an entry name they can reduce the number of potential candidates dramatically by typing just a few characters. This is significantly different from other controls that only scroll to entries that start with what you type in, for example. In this article we'll see how the auto-complete box works and what options it offers; and although most people associate the control with string values only, we'll see that it's capable of handling far more than that and that it can work with any kind of data with little effort.</p> <p>As always, you can download the complete source code at the end of the article. It contains all of the following samples as individual projects.</p> <h3></h3> <h3>Basic usage</h3> <p>For the simplest scenario, you only need to provide the auto-complete box with an items source and process changes of the SelectedItem property either through data binding or by handling the SelectionChanged event. The source items do not necessarily need to be strings like in the following example. The control will automatically call the ToString() method on the items to create the text entries for the drop-down list, so if your data items override this method and a string representation is sufficient, you don't need to do anything else to get a working result. Alternatively, you can also hint the auto-complete box at what property shall be used for the string representation, as we'll see in a moment.</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd"><</span><span class="html">StackPanel</span> <span class="attr">x:Name</span><span class="kwrd">="LayoutRoot"</span> <span class="attr">Background</span><span class="kwrd">="White"</span> <span class="attr">Width</span><span class="kwrd">="100"</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Text</span><span class="kwrd">="{Binding ElementName=MyAutoCompleteBox, <pre class="alt"><span class="lnum" id="lnum3"> </span> Path=SelectedItem, </pre> <pre class="alteven"><span class="lnum" id="lnum4"> </span> TargetNullValue='No item selected', </pre> <pre class="alt"><span class="lnum" id="lnum5"> </span> StringFormat='Selected Item: {0}'}"<span class="kwrd">/></span> </pre> </span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox</span> <span class="attr">x:Name</span><span class="kwrd">="MyAutoCompleteBox"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> <span class="attr">ItemsSource</span><span class="kwrd">="{Binding Items}"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span><span class="kwrd"></</span><span class="html">StackPanel</span><span class="kwrd">></span></pre> <!--CRLF--></div> </div> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span>Items = <span class="kwrd">new</span> List<<span class="kwrd">string</span>>();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>Items.Add(<span class="str">"One"</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span>Items.Add(<span class="str">"Two"</span>);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span>Items.Add(<span class="str">"Three"</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span>Items.Add(<span class="str">"Four"</span>);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span>Items.Add(<span class="str">"Five"</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span>Items.Add(<span class="str">"Six"</span>);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span>Items.Add(<span class="str">"Seven"</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span>Items.Add(<span class="str">"Eight"</span>);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span>Items.Add(<span class="str">"Nine"</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span>Items.Add(<span class="str">"Ten"</span>);</pre> <!--CRLF--></div> </div> <p>This sample code will result in the following:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_2.png"><img width="128" height="102" title="image" style="border:0px solid; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_thumb.png" /></a></p> <p>Surprisingly many aspects of the behavior of this control can be changed by built-in properties, with one of the most important ones being the way the auto-complete box filters the entries.</p> <h4></h4> <h4>Changing the filter mode</h4> <p>By default, the auto-complete box filters entries by a "starts with" pattern. This means that if the string representation of an item starts with what is typed into the text box, it will be listed in the drop-down area. You can change this behavior with the FilterMode property of the control, which can be one of the predefined values of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletefiltermode(v=VS.95).aspx" target="_blank">AutoCompleteFilterMode enumeration</a>. By changing this property, you can change the match algorithm to a "contains" or "equals" pattern and also use case-sensitive filtering instead of the default case-insensitive behavior.</p> <h4>Automatic text completion</h4> <p>One interesting property is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.istextcompletionenabled(v=VS.95).aspx" target="_blank">IsTextCompletionEnabled</a> parameter. If you set this to true, the first possible match in the drop-down list is automatically selected and displayed in the textbox. To preserve fluent typing capabilities, the part of the match that wasn't typed in by the user is selected, so they can overwrite it if they just keep typing.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_4.png"><img width="435" height="165" title="image" style="border:0px solid; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_thumb_1.png" /></a></p> <p>One drawback of this is that this actually changes the SelectedItem property whenever a different match from the drop-down list is selected and put into the text box. This is potentially undesired, because it might trigger additional (expensive or otherwise time-consuming) logic in your (view) model. Maybe that is the reason why the automatic completion is turned off by default.</p> <h4>Other behavioral properties</h4> <p>If you have a very large list of source items looking up potential matches may be expensive, or might result in a similar large list which doesn't really help the user much. In these cases, you can restrict the number of minimum characters the user has to type in until the drop-down list is populated with possible matches. If you set the corresponding property <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.minimumprefixlength(v=VS.95).aspx" target="_blank">MinimumPrefixLength</a> to a value of 3 for example, no list will be shown if the user has typed in less than three characters. The default value is 0.</p> <p>A similar property is <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.minimumpopulatedelay(v=VS.95).aspx" target="_blank">MinimumPopulateDelay</a> (again the default value is 0) which allows to set the number of milliseconds that must expire after the user has typed in some text to kick of the population of the drop-down list. This is particularly useful in scenarios where you fetch the data for the drop-down list from a web service or similar (see below) and you need to minimize the number of network round-trips.</p> <h4></h4> <h4>Styling of the AutoCompleteBox</h4> <p>Of course you can change the control template of the auto-complete box to change major parts of it. But in addition, the control exposes three properties that you can use directly to change the visual appearance of some of its inner parts more easily. Those three properties are: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.textboxstyle(v=VS.95).aspx" target="_blank">TextBoxStyle</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.itemcontainerstyle(v=VS.95).aspx" target="_blank">ItemContainerStyle</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.itemtemplate(v=VS.95).aspx" target="_blank">ItemTemplate</a>. They allow you to provide individual styles for the text box and the list box items. More advanced properties of the contained list box (like the items panel for example) are not exposed directly, but these three properties give you a lot of flexibility already. I won't go into the details of styling the control, as it's the same as styling stand-alone text boxes and list boxes, and also the linked MSDN documentation for the text box and item container style properties contain a full example for that. We'll use the item template to customize the control in the following parts though.</p> <p><em>Advanced note: Although the auto-complete box very much behaves like an items control and offers some of the same properties like items source and item template, it actually does not inherit from the items control base class like the combo box does, for example. The reason for this is that the auto-complete box does not have a strong relation to a particular control for the drop-down part. The only requirement is that this selector implements the </em><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.iselectionadapter(v=VS.95).aspx" target="_blank"><em>ISelectionAdapter interface</em></a><em>. This means that you can change the default list box used for this to any control, even custom controls you've developed yourself. An auto-complete box using a tree view or similar is definitely a possible scenario. This gives you even more flexibility when you need to customize the control. However, this is an advanced topic that requires deriving from the auto-complete box and creating the actual selection adapter which I won't cover in this article. </em></p> <h3>Using custom data and an item template</h3> <p>Like I already pointed out you can use the auto-complete box with non-string data. This includes data that you want to present in a visually appealing way with more than just simple text entries. To this end, you can use the item template to specify how the items in the drop-down list will appear. Take the following simple model, for example:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> MyData</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> Uri ImageUri</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> get;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> set;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> <span class="kwrd">public</span> <span class="kwrd">string</span> Name</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> get;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> set;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span>}</pre> <!--CRLF--></div> </div> <p>To show the images referenced in these items, you can create an item template like the following:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox</span> <span class="attr">ItemsSource</span><span class="kwrd">="{Binding Data}"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> <span class="attr">ValueMemberPath</span><span class="kwrd">="Name"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="attr">FilterMode</span><span class="kwrd">="ContainsOrdinal"</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox.ItemTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <span class="kwrd"><</span><span class="html">DataTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd"><</span><span class="html">StackPanel</span> <span class="attr">Orientation</span><span class="kwrd">="Horizontal"</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> <span class="kwrd"><</span><span class="html">Image</span> <span class="attr">Source</span><span class="kwrd">="{Binding ImageUri}"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Text</span><span class="kwrd">="{Binding Name}"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> <span class="attr">VerticalAlignment</span><span class="kwrd">="Center"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> <span class="attr">Margin</span><span class="kwrd">="10 0 0 0"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> <span class="kwrd"></</span><span class="html">StackPanel</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="kwrd"></</span><span class="html">DataTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> <span class="kwrd"></</span><span class="html">sdk:AutoCompleteBox.ItemTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span><span class="kwrd"></</span><span class="html">sdk:AutoCompleteBox</span><span class="kwrd">></span></pre> <!--CRLF--></div> </div> <p>An important detail to note here is the use of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.valuememberpath(v=VS.95).aspx" target="_blank">ValueMemberPath</a> property. When you work with complex types you need to tell the control what property should be used for a) the display in the text box, and b) in the filtering process for the items in the drop-down list. In this particular sample, we show both the image and name in the drop-down list, but in the text box and for the filtering, only the name property of our data class is used. Alternatively you can use the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.valuememberbinding(v=VS.95).aspx" target="_blank">ValueMemberBinding</a> property to use a binding instead of a property path, for example when you want to use a value converter. The result of the above example is something like this:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_6.png"><img width="184" height="140" title="image" style="border:0px solid; background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/Users/MisterGoodcat/__image_thumb_2.png" /></a></p> <h3></h3> <h3>Using a custom filter method</h3> <p>In some cases, the built-in capabilities of the auto-complete box may not be sufficient for your scenario. Imagine a situation where you want to filter your list of source items by multiple property values. For example, users may want to search a database of electronic components by both the part name or the part number. Then you would have to compare the input of the text box to both properties to determine which possible candidates should be visible in the drop-down list. Luckily, the control offers two extension points for that too: the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.textfilter(v=VS.95).aspx" target="_blank">TextFilter</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.itemfilter(v=VS.95).aspx" target="_blank">ItemFilter</a> properties. Both offer the possibility to provide custom methods that are called to filter your items source. The first one passes in the string representation of the item to test, the second one in contrast passes in the original items used to bind the control in the first place. Let's see an example of how this works.</p> <p>I'm using the same data class as in the sample before. This time, the XAML looks slightly different though:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox</span> <span class="attr">x:Name</span><span class="kwrd">="MyAutoCompleteBox"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> <span class="attr">ItemsSource</span><span class="kwrd">="{Binding Data}"</span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="attr">ValueMemberPath</span><span class="kwrd">="Name"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <span class="attr">ItemFilter</span><span class="kwrd">="{Binding FilterItem}"</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox.ItemTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd"><</span><span class="html">DataTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> <span class="kwrd"><</span><span class="html">StackPanel</span> <span class="attr">Orientation</span><span class="kwrd">="Horizontal"</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> <span class="kwrd"><</span><span class="html">Image</span> <span class="attr">Source</span><span class="kwrd">="{Binding ImageUri}"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Text</span><span class="kwrd">="{Binding Name}"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> <span class="attr">VerticalAlignment</span><span class="kwrd">="Center"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> <span class="attr">Margin</span><span class="kwrd">="10 0 0 0"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="kwrd"></</span><span class="html">StackPanel</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> <span class="kwrd"></</span><span class="html">DataTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> <span class="kwrd"></</span><span class="html">sdk:AutoCompleteBox.ItemTemplate</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span><span class="kwrd"></</span><span class="html">sdk:AutoCompleteBox</span><span class="kwrd">></span> </pre> <!--CRLF--></div> </div> <p>Note that I bind the ItemFilter property to a property of my view model here. This property is just a simple wrapper around the actual filter method:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> AutoCompleteFilterPredicate<<span class="kwrd">object</span>> FilterItem</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> get</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <span class="kwrd">return</span> DoFilterItem;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span>}</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span><span class="kwrd">private</span> <span class="kwrd">bool</span> DoFilterItem(<span class="kwrd">string</span> search, <span class="kwrd">object</span> data)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> <span class="rem">// get the original item</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> var myData = data <span class="kwrd">as</span> MyData;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> <span class="kwrd">if</span> (myData == <span class="kwrd">null</span>)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> <span class="kwrd">return</span> <span class="kwrd">false</span>;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span>  </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> <span class="rem">// list the item if the name length is equal </span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> <span class="rem">// to the search term length</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum21"> </span> <span class="kwrd">return</span> search.Length == myData.Name.Length;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum22"> </span>}</pre> <!--CRLF--></div> </div> <p>As you can see, the method signature for the filtering takes the search term and the current item to inspect as arguments. You need to return a bool value that determines whether the item will be included in the drop-down list (true) or not (false). In this case, I only return those items whose names have the same length like the search term. Not very meaningful, but it shows how you can do filtering the auto-complete box does not support out of the box. The formal documentation for the filter methods is described <a href="http://msdn.microsoft.com/en-us/library/dd833750(v=VS.95).aspx" target="_blank">here</a>.</p> <h3>Asynchronous filtering</h3> <p>One thing people frequently ask is how to do asynchronous filtering. A particular requirement often is to fetch possible candidates for the drop-down list from a web service. Doing this is surprisingly easy with the auto-complete box. To aid this, it offers two things. First of all there is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.populating(v=VS.95).aspx" target="_blank">Populating</a> event. It is raised when the population of the drop-down list is about to start. The important part is that you have the possibility to cancel the populating by setting the appropriate property of the event arguments in your event handler. This is the optimal place and way to start your asynchronous service call. The second piece of the puzzle is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.populatecomplete(v=VS.95).aspx" target="_blank">PopulateComplete</a> method. Call this method to signal to the auto-complete box that you are finished setting up the items source and that it can show the drop-down list now. In the end, this comes down to very little code, like this:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">private</span> <span class="kwrd">void</span> MyAutoCompleteBox_Populating(<span class="kwrd">object</span> sender, PopulatingEventArgs e)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="rem">// create a web client and initiate the service call</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> FilterServiceClient client = <span class="kwrd">new</span> FilterServiceClient();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> client.FilterCompleted += <span class="kwrd">new</span> EventHandler<FilterCompletedEventArgs>(ServiceClient_FilterCompleted);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> client.FilterAsync(e.Parameter);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> <span class="rem">// cancel populating</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> e.Cancel = <span class="kwrd">true</span>;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span>}</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span><span class="kwrd">private</span> <span class="kwrd">void</span> ServiceClient_FilterCompleted(<span class="kwrd">object</span> sender, FilterCompletedEventArgs e)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span>{</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> <span class="rem">// set up the new items source</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> <span class="rem">// and tell the auto-complete box to finish populating</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> MyAutoCompleteBox.ItemsSource = e.Result;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span> MyAutoCompleteBox.PopulateComplete();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span>}</pre> <!--CRLF--></div> </div> <p>As you can see, all that happens is the web service call is started in the populating event, and when it returns, the auto-complete box is told to finish the previously canceled populating. The corresponding XAML for this is:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox</span> <span class="attr">x:Name</span><span class="kwrd">="MyAutoCompleteBox"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> <span class="attr">MinimumPopulateDelay</span><span class="kwrd">="500"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="attr">Populating</span><span class="kwrd">="MyAutoCompleteBox_Populating"</span> <span class="kwrd">/></span></pre> <!--CRLF--></div> </div> <p>Please note the use of the MinimumPopulateDelay property I've discussed above. To avoid that my service is being hit on every keystroke I've added a delay of 500 milliseconds here. This means that the populating event will not be raised more often than once per half second. </p> <h4>Bonus: MVVM-friendly asynchronous filtering</h4> <p>The way the above mentioned procedure works is not very MVVM friendly. You have the possibility to bind commands of your view model to events with built-in helpers like triggers and actions (or probably your MVVM framework has its own helpers for this); however, for the auto-complete box this is not enough. You need to change a property value of the very specific populating event arguments as well as call a method on the control when you have finished the filtering process. To avoid tight coupling between the view model and auto-complete box (as it would be introduced by passing references of event arguments or the control itself to your view model) I have written a behavior that acts as the glue between both. This only is one of many possibilities to handle this situation, but for my requirements so far it was sufficient.</p> <p>First of all I created a parameter data container class to pass around the required information. In particular, the FilterCriteria property will contain the string the user entered into the text box. The FilterComplete property is an abstraction of the PopulateComplete method the auto-complete box offers.</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> FilterAsyncParameters</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> Action FilterComplete</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> get;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd">private</span> set;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> <span class="kwrd">public</span> <span class="kwrd">string</span> FilterCriteria</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> get;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="kwrd">private</span> set;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span>  </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> <span class="kwrd">public</span> FilterAsyncParameters(Action filterComplete, <span class="kwrd">string</span> filterCriteria)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span> FilterComplete = filterComplete;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span> FilterCriteria = filterCriteria;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span>}</pre> <!--CRLF--></div> </div> <p>Next I created a behavior that is typed to the AutoCompleteBox. This behavior has a dependency property of type ICommand that can be used to bind a command of the view model which should be executed when asynchronous filtering has to be performed.</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> FilterAsyncBehavior : Behavior<AutoCompleteBox></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> ICommand FilterAsyncCommand</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> get</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> <span class="kwrd">return</span> (ICommand)GetValue(FilterAsyncCommandProperty);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> set</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> SetValue(FilterAsyncCommandProperty, <span class="kwrd">value</span>);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">readonly</span> DependencyProperty FilterAsyncCommandProperty = DependencyProperty.Register(<span class="str">"FilterAsyncCommand"</span>, </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> <span class="kwrd">typeof</span>(ICommand), </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span> <span class="kwrd">typeof</span>(FilterAsyncBehavior), </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span> <span class="kwrd">new</span> PropertyMetadata(<span class="kwrd">null</span>));</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> ...</pre> <!--CRLF--></div> </div> <p>The behavior overrides the OnAttached and OnDetaching methods to add and remove an event handler for the populating event of the associated auto-complete box. In the event handler, the populating is canceled and the command on the view model is executed. The behavior creates a new instance of the custom parameters class to pass both the search string as well as the PopulateComplete method (disguised as FilterComplete action) to the view model.</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnAttached()</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">base</span>.OnAttached();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <span class="rem">// handle the populating event of the associated auto complete box</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> AssociatedObject.Populating += AssociatedObject_Populating;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span>}</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span><span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnDetaching()</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> <span class="rem">// detach the event handler</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> AssociatedObject.Populating -= AssociatedObject_Populating;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> <span class="kwrd">base</span>.OnDetaching();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span>}</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span>  </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span><span class="kwrd">private</span> <span class="kwrd">void</span> AssociatedObject_Populating(<span class="kwrd">object</span> sender, PopulatingEventArgs e)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> <span class="rem">// get the command</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> ICommand filterCommand = FilterAsyncCommand;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum21"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum22"> </span> <span class="kwrd">if</span> (filterCommand != <span class="kwrd">null</span>)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum23"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum24"> </span> <span class="rem">// create the parameters for the command</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum25"> </span> var parameters = <span class="kwrd">new</span> FilterAsyncParameters(AssociatedObject.PopulateComplete, e.Parameter);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum26"> </span>  </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum27"> </span> <span class="rem">// execute command</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum28"> </span> filterCommand.Execute(parameters);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum29"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum30"> </span> <span class="rem">// cancel the population of the auto complete box</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum31"> </span> e.Cancel = <span class="kwrd">true</span>;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum32"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum33"> </span>}</pre> <!--CRLF--></div> </div> <p>That's basically all. You can then use the behavior in XAML like this:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd"><</span><span class="html">sdk:AutoCompleteBox</span> <span class="attr">x:Name</span><span class="kwrd">="MyAutoCompleteBox"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> <span class="attr">MinimumPopulateDelay</span><span class="kwrd">="500"</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="attr">ItemsSource</span><span class="kwrd">="{Binding Data}"</span><span class="kwrd">></span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <span class="kwrd"><</span><span class="html">interactivity:Interaction.Behaviors</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <span class="kwrd"><</span><span class="html">utilities:FilterAsyncBehavior</span> <span class="attr">FilterAsyncCommand</span><span class="kwrd">="{Binding FilterAsyncCommand}"</span> <span class="kwrd">/></span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd"></</span><span class="html">interactivity:Interaction.Behaviors</span><span class="kwrd">></span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span><span class="kwrd"></</span><span class="html">sdk:AutoCompleteBox</span><span class="kwrd">></span></pre> <!--CRLF--></div> </div> <p>Of course your view model has to provide a property and some kind of implementation for the FilterAsyncCommand. In my case, I've used the RelayCommand class of MVVM light to relay execution to a method in my view model. The code is pretty similar to what we had in the code-behind file of the main page before, but of course without interacting with the auto-complete box directly. </p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> MainViewModel()</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> FilterAsyncCommand = <span class="kwrd">new</span> RelayCommand<FilterAsyncParameters>(ExecuteFilterAsync);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span>}</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span><span class="kwrd">private</span> <span class="kwrd">void</span> ExecuteFilterAsync(FilterAsyncParameters args)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span>{</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> <span class="rem">// start the async web service call</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> FilterServiceClient client = <span class="kwrd">new</span> FilterServiceClient();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> client.FilterCompleted += <span class="kwrd">new</span> EventHandler<FilterCompletedEventArgs>(ServiceClient_FilterCompleted);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> client.FilterAsync(args.FilterCriteria, args);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span>}</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span><span class="kwrd">private</span> <span class="kwrd">void</span> ServiceClient_FilterCompleted(<span class="kwrd">object</span> sender, FilterCompletedEventArgs e)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span>{</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> <span class="rem">// set the new data </span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span> Data = e.Result;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> <span class="rem">// trigger the filter complete action</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> var filterArgs = e.UserState <span class="kwrd">as</span> FilterAsyncParameters;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum21"> </span> filterArgs.FilterComplete();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum22"> </span>}</pre> <!--CRLF--></div> </div> <p>So, with little effort you can remove all code-behind even for asynchronous filtering and provide a more MVVM-friendly version of the sample.</p> <h3></h3> <h3>Summary</h3> <p>Hopefully this article has helped you realize that with the AutoCompleteBox really comes great flexibility. The built-in features already cover a lot of the most common situations, and it offers various extension points for customization and to plug in your own filter logic, even for advanced scenarios when you want to fetch the potential matches from a web service. You can download the complete source code that contains all the sample projects here:</p> <p><a href="http://silverlightshow.net/Storage/Sources/AutoCompleteBox_Projects.zip" target="_blank">Download source code</a></p> <h3>About the author</h3> <p>Peter Kuhn aka "Mister Goodcat" has been working in the IT industry for more than ten years. After being a project lead and technical director for several years, developing database and device controlling applications in the field of medical software and bioinformatics as well as for knowledge management solutions, he founded his own business in 2010. Starting in 2001 he jumped onto the .NET wagon very early, and has also used Silverlight for business application development since version 2. Today he uses Silverlight, .NET and ASP.NET as his preferred development platforms and offers training and consulting around these technologies and general software design and development process questions. In his free time he also works on hobby and commercial game projects. You can find his tech blog here: <a href="http://www.pitorque.de/MisterGoodcat/">http://www.pitorque.de/MisterGoodcat/</a></p> http://www.silverlightshow.net/items/Using-the-AutoCompleteBox.aspx editorial@silverlightshow.net (Peter Kuhn ) http://www.silverlightshow.net/items/Using-the-AutoCompleteBox.aspx#comments http://www.silverlightshow.net/items/Using-the-AutoCompleteBox.aspx Mon, 31 Jan 2011 01:01:00 GMT Getting ready for Microsoft Silverlight Exam 70-506 (Part 2) <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;"><strong>SilverlightShow</strong> and <strong>Gill Cleeren</strong> start a <strong>series of materials</strong> aimed at helping you get prepared for taking <a href="http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us" target="_blank">Microsoft Silverlight Exam 70-506</a>. Through this series we will try to structure the resources available on the internet, grouping them by topic covered in the exam. <strong>Any feedback would be much appreciated</strong>! Thanks!  </div> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More resources...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Free SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Search.aspx?q=+RichTextBox&adv=false">WCF RIA Services series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx">'Getting Ready for Microsoft Silverlight Exam 70-506' Ebook </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx"><img style="border:0px solid;" alt="Getting Ready for Microsoft Silverlight Exam 70-506: Ebook" src="http://www.silverlightshow.net/Storage/sl_exam_thumb_small.png" usemap="#rade_img_map_1291385581316" /></a><br /> <strong><span style="font-size: 13px;">($9.99)</span></strong></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>This article is Part 2 of the series on Microsoft Silverlight Exam.</p> <p>In the first part of this article series, we focused on learning the skills required for the layout-related questions and topics in the Silverlight exam (70-506). We looked at the layout containers (such as the Grid and the ScrollViewer), the basic controls (such as the TextBlock, CheckBox and the Button), how to create user controls, the navigation framework, displaying collections in controls such as the ListBox and finally at how the MediaElement can be used to playback media.</p> <p>In this part, we’ll stay in the say theme let’s say: we’ll look at Enhancing the User Interface. We’ll be covering styling, templating, visual state manager, animations and more. For your convenience, the following list contains links to the other parts of the article series which have been finished already: </p> <ul> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-the-exams-Part-1.aspx">Laying Out a User Interface (15%)</a> </li> <li><strong>Enhancing the User Interface (14%)</strong> <strong>-> this part</strong> </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-3.aspx">Implementing Application Logic (16%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-4.aspx">Working with Data (17%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-5.aspx">Interacting with a Host Platform (11%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-6.aspx">Structuring Applications (13%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-7.aspx">Deploying Applications (13%)</a>  </li> </ul> <h4>Part 2: Enhancing the User Interface</h4> <p>Since Silverlight is the platform of choice for building rich internet applications (RIAs), building a basic interface with just plain buttons and no animations seems like under-using the capabilities. Silverlight has many capabilities on board to help us create a unique experience, tailored to the business needs. Customizing the application’s UI has never been easier, since several features inside Silverlight are just targeted at doing so. In this part, we’ll look at how we can customize the UI of the application, so that our application can really shine!</p> <h5>Create or modify control styles</h5> <p>When we start creating a Silverlight application, we’re tempted to place UI-related properties directly on the elements themselves. Properties like FontSize, Width and Fill are often created on the declaration of the element in any XAML file. While there’s nothing really wrong with this way of working, it can become a difficult task to manage this in a later phase. Assume you have created an application for a company that has blue as its branding color. At some point, they want to update their logos and colors and everything needs to become red. You as the developer need to go through all the code files to make the changes directly in all XAML files. It will take some time.</p> <p>For this very purpose, styles exist. Similar to CSS styling in HTML, styles in Silverlight are a way to create a common location for UI-related properties. A style also promotes reusability: when using the same style in all views, a simple change to that style changes all views without any manual change in the views themselves. </p> <p>While styles are similar to CSS, there are some important differences. The ‘C’ in CSS stands for cascading and basically means that the final style of an element is the combination of all styles that are being applied on the element. This is not the case in Silverlight’s styling. Styles in Silverlight can however inherit.</p> <p>To learn more about styling, take a look at the following articles:</p> <ul> <li>Setting a style locally / setting a style at the page control level / setting a style globally <br /> <em>Creating a style for local use, page level use or global use is similar. The only thing that’s different is of course the location where the style gets declared. This influences of course the scope that the style has. If a style gets declared on a Grid, only elements within that Grid are able to use the style. If the style is declared on the page/user control level, all controls within that page/user control can access the style. Controls on other pages however cannot access the style. If we want globally-known styles, we can declare them on the application level (in the resources section of the App.xaml). <br /> If a control requires a specific style, XAML tree walking is executed to locate the style: first in its parent, then its parent… until the page/user control level is reached. If the style can’t be located on that level either, the style is searched for on the application level. The fact that styles in Silverlight aren’t cascading follows from this: once the style is located on a specific level, higher levels in the XAML tree aren’t searched for anymore. It’s therefore possible to create a style with the same name on both the page and application level if this should be required.</em> <ul> <li><a href="http://www.silverlightshow.net/news/Silverlight-Tutorials-Styling-and-Templating.aspx">http://www.silverlightshow.net/news/Silverlight-Tutorials-Styling-and-Templating.aspx</a> </li> <li><a href="http://www.silverlightshow.net/shows/Control-Customization-in-Silverlight-2.aspx">http://www.silverlightshow.net/shows/Control-Customization-in-Silverlight-2.aspx</a> (video, contains information about styling controls among other things) </li> <li>Expression Blend blog: Styling common controls: <a href="http://blogs.msdn.com/b/expression/archive/2009/09/09/silverlight-control-styling-tips-articles.aspx">http://blogs.msdn.com/b/expression/archive/2009/09/09/silverlight-control-styling-tips-articles.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Styling-Silverlight-UI-Elements.aspx">http://www.silverlightshow.net/news/Styling-Silverlight-UI-Elements.aspx</a> (video) </li> <li>Styling a DataGrid: <a href="http://www.silverlightshow.net/news/WPF-DataGrid.aspx">http://www.silverlightshow.net/news/WPF-DataGrid.aspx</a> </li> <li><a href="http://www.visiblox.com/blog/2010/08/styles-in-silverlight-further-advanced-topics-2">http://www.visiblox.com/blog/2010/08/styles-in-silverlight-further-advanced-topics-2</a> </li> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-4-using-style-elements-to-better-encapsulate-look-and-feel.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-4-using-style-elements-to-better-encapsulate-look-and-feel.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Styling-Part-1.aspx">http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Styling-Part-1.aspx</a> </li> <li><a href="http://weblogs.asp.net/dwahlin/archive/2008/10/23/adding-style-to-silverlight-2-controls.aspx">http://weblogs.asp.net/dwahlin/archive/2008/10/23/adding-style-to-silverlight-2-controls.aspx</a> </li> </ul> </li> <li>Implicit styles: <br /> <em>Implicit or default styles make it possible to create a style that is used for all controls of a specific type, unless a specific style is defined on the control.</em> <ul> <li><a href="http://www.silverlightshow.net/news/Implicit-styling-in-Silverlight-4.aspx">http://www.silverlightshow.net/news/Implicit-styling-in-Silverlight-4.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx">http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-4-Quick-Tip-Styling-application-.aspx">http://www.silverlightshow.net/news/Silverlight-4-Quick-Tip-Styling-application-.aspx</a> </li> </ul> </li> <li>Based-on (inheriting) styles: <br /> <em>It’s possible to create a base style and have other styles inherit from this style. This is achieved using the BasedOn property. </em> <ul> <li><a href="http://www.silverlightshow.net/tips/Tip-Cascading-Styles-in-Silverlight-3.aspx">http://www.silverlightshow.net/tips/Tip-Cascading-Styles-in-Silverlight-3.aspx</a> </li> <li><a href="http://10rem.net/blog/2009/03/18/silverlight-3-%E2%80%93-basedon-styles">http://10rem.net/blog/2009/03/18/silverlight-3-%E2%80%93-basedon-styles</a> </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-how-to-Inherit-from-an-Implicit-Style-.aspx">http://www.silverlightshow.net/news/Silverlight-how-to-Inherit-from-an-Implicit-Style-.aspx</a> </li> <li><a href="http://www.wintellect.com/CS/blogs/jprosise/archive/2009/04/08/silverlight-3-s-new-style-enhancements.aspx">http://www.wintellect.com/CS/blogs/jprosise/archive/2009/04/08/silverlight-3-s-new-style-enhancements.aspx</a> (includes information on Merged Dictionaries as well) </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-4-Implicit-Styles-and-Themes-.aspx">http://www.silverlightshow.net/news/Silverlight-4-Implicit-Styles-and-Themes-.aspx</a> </li> </ul> </li> <li>Merged Dictionaries: <br /> <em>Placing all styles inside the App.xaml’s resource block may become a bit too much in the long run. The file may simple become too large. Merged Dictionaries allow us to split out the style information in several files.</em> <ul> <li><a href="http://www.silverlightshow.net/news/Organizing-Color-Themes-For-Styles-Using-Merged-ResourceDictionaries-In-Silverlight-3.aspx">http://www.silverlightshow.net/news/Organizing-Color-Themes-For-Styles-Using-Merged-ResourceDictionaries-In-Silverlight-3.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Merged-Resource-Dictionaries.aspx">http://www.silverlightshow.net/news/Merged-Resource-Dictionaries.aspx</a> </li> </ul> </li> </ul> <h5>Create control templates</h5> <p>With styling of elements, we can only get to a level where we change the ‘standard’ properties. We won’t however be able to customize the controls to our needs. The most basic example I always tell people is that there’s no option to create a round button just using styling. Last time I checked, there was no “IsRound” property available on the Button class!</p> <p>For this very purpose, the concept of templating exists. With templates, we can completely overrule the visual of a control, we can in other words redefine the template of the control (hence the name templating). Templates support the concept of a ContentPresenter, which is a place holder for the actual content of the control to be placed in. Using TemplateBindings, we can create a binding to pass data from the control instance to the template.</p> <p>The following articles and blog posts can be helpful in exploring the templating functionality in Silverlight:</p> <ul> <li>Creating a template / attaching a template: <ul> <li>ControlTemplate class on MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate%28v=vs.95%29.aspx</a> </li> <li><a href="http://wsinsiders.com/2010/01/30/silverlight-tutorials-styling-and-templating/">http://wsinsiders.com/2010/01/30/silverlight-tutorials-styling-and-templating/</a> (same link as before, this link contains an overview for both styles and templates) </li> <li><a href="http://www.mindscapehq.com/blog/index.php/2010/08/16/silverlight-control-templates/">http://www.mindscapehq.com/blog/index.php/2010/08/16/silverlight-control-templates/</a> </li> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-7-using-control-templates-to-customize-a-control-s-look-and-feel.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-7-using-control-templates-to-customize-a-control-s-look-and-feel.aspx</a> </li> <li><a href="http://www.silverlight.net/learn/quickstarts/controltemplates/">http://www.silverlight.net/learn/quickstarts/controltemplates/</a> </li> <li><a href="http://channel9.msdn.com/Blogs/mtaulty/Silverlight-Templating-a-Button">http://channel9.msdn.com/Blogs/mtaulty/Silverlight-Templating-a-Button</a> (video) </li> <li><a href="http://channel9.msdn.com/Shows/SilverlightTV/Templating-Controls-with-Silverlight-Silverlight-TV-36">http://channel9.msdn.com/Shows/SilverlightTV/Templating-Controls-with-Silverlight-Silverlight-TV-36</a> (video) </li> </ul> </li> <li>Implementing a content placeholder <br /> <em>A ContentPresenter can be used as a place holder for the actual control content.</em> <ul> <li>ContentPresenter class on MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx">http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx</a> </li> <li><a href="http://mstechkb.blogspot.com/2010/10/contentpresenter-content-in-silverlight.html">http://mstechkb.blogspot.com/2010/10/contentpresenter-content-in-silverlight.html</a> </li> </ul> </li> <li>Implementing template binding <br /> <em>When using a control with a template applied on it, a TemplateBinding can be used to pass the values of properties into the control template. </em> <ul> <li>TemplateBinding class on MSDN: <a href="http://msdn.microsoft.com/en-us/library/cc189062%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/cc189062%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-2.aspx">http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-2.aspx</a> </li> <li><a href="http://msdn.microsoft.com/en-us/magazine/cc721611.aspx#id0430033">http://msdn.microsoft.com/en-us/magazine/cc721611.aspx#id0430033</a> </li> <li><a href="http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx">http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx</a> </li> </ul> </li> </ul> <h5>Create or modify data templates</h5> <p>When displaying a collection of items in a control such as a ListBox, a DataTemplate can be used to enhance the visualization of the items. By default, the ToString() method is used. A somewhat better solution is using the DisplayMemberPath where we can indicate which property needs to be displayed. To create a far better experience, we can use a data template. Such a template is basically a piece of XAML that gets generated for each item in the bound collection. The item will then become the data context for the data template upon generation.</p> <p>Read the following list of articles to learn more about data templates:</p> <ul> <li>Modifying item templates for data controls: <ul> <li>ItemTemplate on MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate%28v=vs.95%29.aspx</a> </li> <li>DataTemplate on MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.datatemplate%28VS.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.datatemplate%28VS.95%29.aspx</a> </li> <li><a href="http://drwpf.com/blog/2008/01/03/itemscontrol-d-is-for-datatemplate/">http://drwpf.com/blog/2008/01/03/itemscontrol-d-is-for-datatemplate/</a> (article targets WPF but is compatible with Silverlight) </li> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx</a> </li> <li><a href="http://www.devlifestyle.net/blogs/articles/archive/2010/08/09/silverlight-4-0-tutorial-4-of-n-listbox-itemtemplate.aspx">http://www.devlifestyle.net/blogs/articles/archive/2010/08/09/silverlight-4-0-tutorial-4-of-n-listbox-itemtemplate.aspx</a> </li> <li><a href="http://weblogs.asp.net/dwahlin/archive/2008/04/03/using-silverlight-2-itemscontrol-templates.aspx">http://weblogs.asp.net/dwahlin/archive/2008/04/03/using-silverlight-2-itemscontrol-templates.aspx</a> </li> </ul> </li> <li>Creating a create template / Creating an update template: <ul> <li><a href="http://www.a2zdotnet.com/View.aspx?Id=134">http://www.a2zdotnet.com/View.aspx?Id=134</a> </li> <li><a href="http://www.silverlightshow.net/news/DataForm-Templates-.aspx">http://www.silverlightshow.net/news/DataForm-Templates-.aspx</a> </li> <li><a href="http://www.packtpub.com/article/working-dataform-microsoft-silverlight-4">http://www.packtpub.com/article/working-dataform-microsoft-silverlight-4</a> </li> </ul> </li> <li>Using a user control as a data template: <ul> <li><a href="http://www.silverlightshow.net/items/Using-A-UserControl-As-A-DataTemplate-in-Silverlight-2-and-Blend-2.aspx">http://www.silverlightshow.net/items/Using-A-UserControl-As-A-DataTemplate-in-Silverlight-2-and-Blend-2.aspx</a> </li> <li><a href="http://rhizohm.net/irhetoric/post/2008/03/31/0a-Using-A-UserControl-As-A-DataTemplate-in-Silverlight-2-and-Blend-20a-.aspx">http://rhizohm.net/irhetoric/post/2008/03/31/0a-Using-A-UserControl-As-A-DataTemplate-in-Silverlight-2-and-Blend-20a-.aspx</a> </li> </ul> </li> </ul> <h5>Manipulate visuals</h5> <p>Once any visual has been created, it can be the target of a transform. This transform can manipulate the control in several formats such as rotation, skewing or even moving it to a different position. All transforms in Silverlight are RenderTransforms. Perspective transform can help creating depth: using such a transform, any 2D object such as a control can be rotated over any of the X, Y or Z axis, creating a 3D effect. This is not to be confused with real 3D: it’s a 2D object in a 3D space.</p> <p>Reading the following articles about transformations can help you along the way:</p> <ul> <li>RenderTransformOrigin: <br /> <em>This sets the starting point for a transformation.</em> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/bb980041%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/bb980041%28v=vs.95%29.aspx</a> </li> </ul> </li> <li>SkewTransform: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/bb979772%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/bb979772%28v=vs.95%29.aspx</a> </li> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.skewtransform%28VS.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.skewtransform%28VS.95%29.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/anavijai/4505/Default.aspx">http://www.c-sharpcorner.com/UploadFile/anavijai/4505/Default.aspx</a> </li> <li><a href="http://vbcity.com/blogs/canoz/archive/2010/04/25/beginning-silverlight-skewtransform.aspx">http://vbcity.com/blogs/canoz/archive/2010/04/25/beginning-silverlight-skewtransform.aspx</a> </li> </ul> </li> <li>RotateTransform: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform%28v=vs.95%29.aspx</a> </li> <li><a href="http://vbcity.com/blogs/canoz/archive/2010/04/25/beginning-silverlight-rotatetransform.aspx">http://vbcity.com/blogs/canoz/archive/2010/04/25/beginning-silverlight-rotatetransform.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/anavijai/4476/Default.aspx">http://www.c-sharpcorner.com/UploadFile/anavijai/4476/Default.aspx</a> </li> </ul> </li> <li>ScaleTransform: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.scaletransform%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.scaletransform%28v=vs.95%29.aspx</a> </li> <li><a href="http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-scaletransform.aspx">http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-scaletransform.aspx</a> </li> </ul> </li> <li>TranslateTransform: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.translatetransform%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.translatetransform%28v=vs.95%29.aspx</a> </li> <li><a href="http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-translatetransform.aspx">http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-translatetransform.aspx</a> </li> </ul> </li> <li>CompositeTransform: <br /> <em>The CompositeTransform was added with Silverlight 4 and makes it easier to create a combination of other transformations on an element.</em> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.compositetransform%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.compositetransform%28v=vs.95%29.aspx</a> </li> <li><a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-compositetransform.aspx">http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-compositetransform.aspx</a> </li> <li><a href="http://adamkinney.wordpress.com/2009/11/22/compositetransform-simplifies-transforms-in-silverlight-4-beta/">http://adamkinney.wordpress.com/2009/11/22/compositetransform-simplifies-transforms-in-silverlight-4-beta/</a> </li> </ul> </li> <li>Perspective transform: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/dd470131%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/dd470131%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Perspective-3D-in-Silverlight-3.aspx">http://www.silverlightshow.net/items/Perspective-3D-in-Silverlight-3.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-3-s-New-Perspective-Transform-.aspx">http://www.silverlightshow.net/news/Silverlight-3-s-New-Perspective-Transform-.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/PlaneProjection-for-3D-Effects-Part-1.aspx">http://www.silverlightshow.net/news/PlaneProjection-for-3D-Effects-Part-1.aspx</a> </li> <li><a href="http://channel9.msdn.com/Blogs/ContinuumNews/Silverlight-PlaneProjection-perspective-transform-tutorial">http://channel9.msdn.com/Blogs/ContinuumNews/Silverlight-PlaneProjection-perspective-transform-tutorial</a> </li> </ul> </li> <li>Applying pixel shaders <ul> <li><a href="http://www.silverlightshow.net/news/Pixel-Shaders.aspx">http://www.silverlightshow.net/news/Pixel-Shaders.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Reflection-Shader-for-Silverlight-3-.aspx">http://www.silverlightshow.net/news/Reflection-Shader-for-Silverlight-3-.aspx</a> </li> <li><a href="http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=145">http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=145</a> </li> <li><a href="http://www.wintellect.com/CS/blogs/jprosise/archive/2009/03/25/silverlight-3-s-new-pixel-shaders.aspx">http://www.wintellect.com/CS/blogs/jprosise/archive/2009/03/25/silverlight-3-s-new-pixel-shaders.aspx</a> </li> <li><a href="http://www.silverlightplayground.org/post/2009/03/22/Silverlight-30-Tile-Effect-with-a-Pixel-Shader.aspx">http://www.silverlightplayground.org/post/2009/03/22/Silverlight-30-Tile-Effect-with-a-Pixel-Shader.aspx</a> </li> </ul> </li> </ul> <h5>Animate visuals</h5> <p>Animations are an integral part of Silverlight. While you may think that animations may not be your cup of tea because you only build business applications, think again. Animations can be helpful in creating a better experience for your users. Think of things such as creating an effect where a page/usercontrol slides in smoothly. This is nothing more than an animation!</p> <p>The animation framework in Silverlight is time-based, not frame-based like in many other technologies. This means that Silverlight takes care of interpolation. What we in the end have to do is specifying what the value of a property should be before and after the animation. Silverlight will then draw the intermediate values of the property. Animations always work on properties, not on objects. These properties need to be dependency properties. Don’t worry though, as most visual properties of Silverlight controls are DPs.</p> <p>Learning about animations can be done using the following links:</p> <ul> <li>Creating storyboards: <ul> <li>StoryBoard class on MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard%28v=vs.95%29.aspx</a> </li> <li><a href="http://msdn.microsoft.com/en-us/library/cc189019%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/cc189019%28v=vs.95%29.aspx</a> </li> <li>Dynamic storyboards: <a href="http://weblogs.asp.net/dwahlin/archive/2008/12/17/handling-dynamic-storyboards-and-animations-in-silverlight-2.aspx">http://weblogs.asp.net/dwahlin/archive/2008/12/17/handling-dynamic-storyboards-and-animations-in-silverlight-2.aspx</a> </li> <li><a href="https://www.silverlight.net/learn/quickstarts/animations/">https://www.silverlight.net/learn/quickstarts/animations/</a> </li> <li><a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#8">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#8</a> </li> <li><a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#16">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#16</a> </li> </ul> </li> <li>Animation types: <ul> <li><a href="http://www.silverlightshow.net/items/Did-You-Know.-There-Are-Four-Basic-Animation-Mechanisms.aspx">http://www.silverlightshow.net/items/Did-You-Know.-There-Are-Four-Basic-Animation-Mechanisms.aspx</a> </li> <li>ColorAnimation: <a href="http://www.switchonthecode.com/tutorials/silverlight-tutorial-color-animations">http://www.switchonthecode.com/tutorials/silverlight-tutorial-color-animations</a> </li> <li>DoubleAnimation: <a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#14">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#14</a> </li> <li>PointAnimation: <a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#15">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#15</a> </li> </ul> </li> <li>Programmatically starting and stopping storyboards: <ul> <li><a href="http://www.silverlightshow.net/items/Silverlight-2-Simple-Animation.aspx">http://www.silverlightshow.net/items/Silverlight-2-Simple-Animation.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/BeginAnimation-method-for-Silverlight.aspx">http://www.silverlightshow.net/items/BeginAnimation-method-for-Silverlight.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/How-To-Build-a-Storyboard-Animation-for-Silverlight-in-C.aspx">http://www.silverlightshow.net/news/How-To-Build-a-Storyboard-Animation-for-Silverlight-in-C.aspx</a> </li> <li><a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#18">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#18</a> </li> </ul> </li> <li>Implementing easing functions <ul> <li><a href="http://www.silverlightshow.net/news/Storyboard-Animation-Easing-Demonstration.aspx">http://www.silverlightshow.net/news/Storyboard-Animation-Easing-Demonstration.aspx</a> </li> <li><a href="http://www.silverlightplayground.org/post/2009/03/18/Silverlight3-Easing-Functions.aspx">http://www.silverlightplayground.org/post/2009/03/18/Silverlight3-Easing-Functions.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/dpatra/EasingFunctionInBlend307102009134910PM/EasingFunctionInBlend3.aspx">http://www.c-sharpcorner.com/UploadFile/dpatra/EasingFunctionInBlend307102009134910PM/EasingFunctionInBlend3.aspx</a> </li> </ul> </li> <li>Using keyframes <ul> <li><a href="http://www.silverlightshow.net/items/Did-You-Know.-There-are-3-Versions-of-Keyframe-Animation.aspx">http://www.silverlightshow.net/items/Did-You-Know.-There-are-3-Versions-of-Keyframe-Animation.aspx</a> </li> <li><a href="http://joel.neubeck.net/2008/01/silverlight-how-to-frame-by-frame-animation/">http://joel.neubeck.net/2008/01/silverlight-how-to-frame-by-frame-animation/</a> </li> <li><a href="http://vbcity.com/blogs/canoz/archive/2010/06/19/beginning-silverlight-animation-part-5-keyframe-animation.aspx">http://vbcity.com/blogs/canoz/archive/2010/06/19/beginning-silverlight-animation-part-5-keyframe-animation.aspx</a> </li> </ul> </li> <li>Triggering storyboard on load <ul> <li><a href="http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#12">http://www.dotnetspark.com/kb/2078-animation-silverlight.aspx#12</a> </li> </ul> </li> <li>Repeating and reversing storyboards: <ul> <li><a href="http://www.silverlightshow.net/items/Repeating-an-animation.aspx">http://www.silverlightshow.net/items/Repeating-an-animation.aspx</a> </li> <li>RepeatBehavior: <a href="http://msdn.microsoft.com/en-us/library/bb980146%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/bb980146%28v=vs.95%29.aspx</a> </li> <li><a href="http://msdn.microsoft.com/en-us/library/cc189019%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/cc189019%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.dotnetfunda.com/tutorials/silverlight/animation.aspx">http://www.dotnetfunda.com/tutorials/silverlight/animation.aspx</a> </li> </ul> </li> <li>Responding to events <ul> <li>Reacting to the Completed event: <a href="http://www.silverlightshow.net/items/Silverlight-Taking-Action.aspx">http://www.silverlightshow.net/items/Silverlight-Taking-Action.aspx</a> </li> </ul> </li> <li>Configuring bitmap caching <ul> <li><a href="http://www.silverlightshow.net/news/Bitmap-Caching-in-Silverlight-3-.aspx">http://www.silverlightshow.net/news/Bitmap-Caching-in-Silverlight-3-.aspx</a> </li> <li><a href="http://www.andybeaulieu.com/Home/tabid/67/EntryID/144/Default.aspx">http://www.andybeaulieu.com/Home/tabid/67/EntryID/144/Default.aspx</a> </li> </ul> </li> </ul> <h5>Implement behaviors</h5> <ul> <li>Behaviors <ul> <li><a href="http://www.silverlightshow.net/items/Exploring-Silverlight-behaviors-look-Ma-no-code.aspx">http://www.silverlightshow.net/items/Exploring-Silverlight-behaviors-look-Ma-no-code.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx">http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx</a> </li> <li><a href="http://blogs.msdn.com/b/deviations/archive/2009/06/06/my-first-silverlight-behavior.aspx">http://blogs.msdn.com/b/deviations/archive/2009/06/06/my-first-silverlight-behavior.aspx</a> </li> <li><a href="http://wildermuth.com/2009/05/16/Writing_Behaviors_for_Silverlight_3_-_Part_1">http://wildermuth.com/2009/05/16/Writing_Behaviors_for_Silverlight_3_-_Part_1</a> </li> </ul> </li> <li>Triggering storyboards by using behaviors <ul> <li><a href="http://csharperimage.jeremylikness.com/2009/10/silverlight-behaviors-and-triggers.html">http://csharperimage.jeremylikness.com/2009/10/silverlight-behaviors-and-triggers.html</a> </li> </ul> </li> <li>Dragging and dropping elements <ul> <li><a href="http://msdn.microsoft.com/en-us/library/cc189066%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/cc189066%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/SL4-Desktop-Drag-and-Drop-Silver-Sky.aspx">http://www.silverlightshow.net/items/SL4-Desktop-Drag-and-Drop-Silver-Sky.aspx</a> </li> <li><a href="http://www.85turns.com/2008/08/13/drag-and-drop-silverlight-example/">http://www.85turns.com/2008/08/13/drag-and-drop-silverlight-example/</a> </li> <li><a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=198">http://www.dotnetcurry.com/ShowArticle.aspx?ID=198</a> </li> </ul> </li> </ul> <h5>Managing Visual State</h5> <p>The Visual State Manager is really important when creating custom controls. Using the VSM, we don’t have to manually create a custom template for a control. Instead, the model specifies that a control can be in a number of states (depending on the control, these states are defined using the TemplateVisualState on the control class). Defining a custom template for a control comes down to defining how a control will look in each of these states. While the VSM model is not required, it’s certainly recommended to be used, since Blend also uses this model. If a designer on your team would create new control templates using Blend, these would also be created using the VSM model.</p> <p>State are grouped in so-called Visual State Groups. A control can be in a combination of states which aren’t part of the same state group. Therefore, these groups are said to be orthogonal. Between states, VisualTransitions can be created. The VSM automatically applies these when defined to create smooth transitions between control states.</p> <p>Take a look at the following articles to learn more about the Visual State Manager:</p> <ul> <li>Visual State Manager: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-4-Using-The-Visual-State-Manager-And-Behaviors.aspx">http://www.silverlightshow.net/news/Silverlight-4-Using-The-Visual-State-Manager-And-Behaviors.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Using-Visual-States-in-Custom-Controls-in-Silverlight.aspx">http://www.silverlightshow.net/news/Using-Visual-States-in-Custom-Controls-in-Silverlight.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Jounce-Part-10-Visual-State-Manager-.aspx">http://www.silverlightshow.net/news/Jounce-Part-10-Visual-State-Manager-.aspx</a> </li> <li><a href="http://blogs.silverlight.net/blogs/justinangel/archive/2008/12/25/custom-vsm-visualstatemanagers-in-silverlight-2-0.aspx">http://blogs.silverlight.net/blogs/justinangel/archive/2008/12/25/custom-vsm-visualstatemanagers-in-silverlight-2-0.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Using-the-Visual-State-Manager-.aspx">http://www.silverlightshow.net/news/Using-the-Visual-State-Manager-.aspx</a> </li> <li><a href="http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm.aspx">http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm.aspx</a> </li> </ul> </li> <li>Creating state groups <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.visualstategroup%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.visualstategroup%28v=vs.95%29.aspx</a> </li> </ul> </li> <li>Transitioning states <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.visualtransition%28VS.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.visualtransition%28VS.95%29.aspx</a> </li> <li><a href="http://msdn.microsoft.com/en-us/magazine/cc721611.aspx">http://msdn.microsoft.com/en-us/magazine/cc721611.aspx</a> </li> </ul> </li> <li>Programmatically changing states </li> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager.gotostate%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager.gotostate%28v=vs.95%29.aspx</a> </li> <li><a href="http://www.codeproject.com/KB/silverlight/VisualStateManagerNavbar.aspx">http://www.codeproject.com/KB/silverlight/VisualStateManagerNavbar.aspx</a> </li> </ul> <h4>Summary</h4> <p>In this second part of the exam preparation guide, we looked at topics related to enhancing the user interface. In the following part, we’ll be focusing more on code!</p> <h4>About Gill</h4> <p>Gill Cleeren is Microsoft Regional Director (<a href="http://www.theregion.com/">www.theregion.com</a>), Silverlight MVP (former ASP.NET MVP), INETA speaker bureau member and Silverlight Insider. He lives in Belgium where he works as .NET architect at Ordina. Passionate about .NET, he’s always playing with the newest bits. In his role as Regional Director, Gill has given many sessions, webcasts and trainings on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd Berlin 2010, TechDays Belgium, DevDays NL, NDC Oslo Norway, SQL Server Saturday Switserland, Spring Conference UK, Silverlight Roadshow in Sweden… He’s also the author of many articles in various developer magazines and for SilverlightShow.net. He organizes the yearly Community Day event in Belgium.</p> <p>He also leads Visug (<a href="http://www.visug.be/">www.visug.be</a>), the largest .NET user group in Belgium. Gill recently published his first book: “<a href="https://www.packtpub.com/microsoft-silverlight-4-data-and-services-cookbook/book">Silverlight 4 Data and Services Cookbook</a>” (Packt Publishing). You can find his blog at <a href="http://www.snowball.be/">www.snowball.be</a>. </p> <p>Twitter: @gillcleeren</p> http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-2.aspx editorial@silverlightshow.net (Gill Cleeren ) http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-2.aspx#comments http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-2.aspx Mon, 24 Jan 2011 11:38:00 GMT Getting ready for Microsoft Silverlight Exam 70-506 (Part 1) <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; padding-top: 5px;"><strong>SilverlightShow</strong> and <strong>Gill Cleeren</strong> start a <strong>series of materials</strong> aimed at helping you get prepared for taking <a href="http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us" target="_blank">Microsoft Silverlight Exam 70-506</a>. Through this series we will try to structure the resources available on the internet, grouping them by topic covered in the exam. <strong>Any feedback would be much appreciated</strong>! Thanks!  </div> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More resources...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Free SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Search.aspx?q=+RichTextBox&adv=false">WCF RIA Services series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx">'Getting Ready for Microsoft Silverlight Exam 70-506' Ebook </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/silverlight_exam.aspx"><img style="border:0px solid;" alt="Getting Ready for Microsoft Silverlight Exam 70-506: Ebook" src="http://www.silverlightshow.net/Storage/sl_exam_thumb_small.png" usemap="#rade_img_map_1291385581316" /></a><br /> <strong><span style="font-size: 13px;">($9.99)</span></strong></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>This article is Part 1 of the series on Microsoft Silverlight Exam. </p> <p>Silverlight is a rapid-evolving technology. Over a time span of 3 years, it has evolved from being a JavaScript-based platform to a full .NET implementation. With 4 versions released and version 5 in the pipeline at the time of writing, Silverlight has more versions than .NET! That makes it hard for book writers such as me to keep up with new features being added.</p> <p>It seems to me that it’s also difficult to create and manage exams about Silverlight. For the previous versions of Silverlight, there weren’t any exams available. Silverlight 4 is the first version of which an exam is being created. It was possible to take the beta-exam late 2010; the exam will be officially open in Q1 2011. More information about the exam can be found here: <a href="http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us">http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us</a></p> <p>With this guide, I aim to make the preparation for the exam a bit easier. </p> <h1>Goal and approach to getting ready</h1> <p>At this point, there’s no official courseware available to study for the exam. In any case, you should therefore be familiar already with Silverlight development. Reading some books on Silverlight will help as well. The list below contains some links to books available for Silverlight 4:</p> <ul> <li><a href="http://www.silverlightshow.net/book/Silverlight-4-Unleashed.aspx" target="_self">Silverlight 4 Unleashed</a> </li> <li><a href="http://www.silverlightshow.net/book/Silverlight-4-in-Action.aspx" target="_self">Silverlight 4 In Action</a> </li> <li><a href="http://www.silverlightshow.net/book/Microsoft-Silverlight-4-Data-and-Services-Cookbook.aspx" target="_self">Microsoft Silverlight 4 Data and Services Cookbook</a> </li> <li><a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx" target="_self">Pro Business Application with Silverlight 4</a> </li> </ul> <p>On top of that, reading articles and blog posts will help you in passing the exam. In this article series, I try to create an extensive list of links along with some explanation on the topic at hand that contain the information you need to know. To structure this, we’ll follow the list of topics described by Microsoft on the exam outline page (<a href="http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us#tab2" target="_blank">http://www.microsoft.com/learning/en/us/Exam.aspx?ID=70-506&Locale=en-us#tab2</a>): </p> <ul> <li><strong>Laying Out a User Interface (15%)  -> this part</strong> </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-2.aspx" target="_self">Enhancing the User Interface (14%) </a></li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-3.aspx">Implementing Application Logic (16%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-4.aspx">Working with Data (17%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-5.aspx">Interacting with a Host Platform (11%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-6.aspx">Structuring Applications (13%)</a>  </li> <li><a href="http://www.silverlightshow.net/items/Getting-ready-for-Microsoft-Silverlight-Exam-70-506-Part-7.aspx">Deploying Applications (13%)</a>  </li> </ul> <p>(Small note: the outlined percentages total 99%, so you’ll probably get 1% for entering your name!)</p> <p>In this first part we’ll look at “Laying Out a User Interface”. </p> <h1>Part 1: Laying Out a User Interface</h1> <h2>Arrange content with panels</h2> <p>Layout is a very important aspect in Silverlight. Since Silverlight is an RIA, applications we build with it are intended to run on different screen resolutions. A well-built application should look fine on all these resolutions as well. Luckily, Silverlight has inherited layout panels from WPF, including the Canvas, the Grid and the StackPanel. Via the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a>, other panels including the DockPanel and the WrapPanel were added. It’s my opinion that the Grid is the most used panel for (business) applications, as it can help lining out content, while a Canvas will be more used for example when creating a game with moving parts or a “hand-drawn” custom control such as a gauge control. The Canvas support z-indexing as well, allowing sending its children up or down in the stack. </p> <p>When looking at the outline, the following articles are helpful:</p> <ul> <li>Layout in general: </li> </ul> <blockquote> <p>Layout controls help building the user interface of Silverlight applications. The class hierarchy for the most important layout controls is as follows:</p> </blockquote><blockquote> <p>System.Object <br /> --System.Windows.DependencyObject <br /> ----System.Windows.UIElement <br /> ------System.Windows.FrameworkElement <br /> -------- System.Windows.Controls.Viewbox <br /> --------System.Windows.Controls.Border <br /> --------System.Windows.Controls.Panel <br /> ----------System.Windows.Controls.Grid <br /> ----------System.Windows.Controls.StackPanel <br /> ----------System.Windows.Controls.Canvas <br /> --------System.Windows.Controls.Control <br /> ----------System.Windows.Controls.ContentControl <br /> ------------System.Windows.Controls.ScrollViewer</p> </blockquote> <ul> <ul> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-2-using-layout-management.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-2-using-layout-management.aspx</a> </li> <li><a href="http://www.silverlight.net/learn/videos/all/silverlight-page-layout-part-1-xaml/">http://www.silverlight.net/learn/videos/all/silverlight-page-layout-part-1-xaml/</a> </li> <li>Under the covers of the Silverlight layout system: <a href="http://10rem.net/blog/2010/03/26/book-excerpt-the-silverlight-layout-system">http://10rem.net/blog/2010/03/26/book-excerpt-the-silverlight-layout-system</a> </li> </ul> <li>The Grid: <br /> <em>A Grid can be used to create a layout structure with rows and columns.</em> <ul> <li>Grid at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.grid(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.grid(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx">http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx">http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx</a> </li> <li><a href="http://www.silverlight.net/learn/videos/all/using-silverlight-grids-and-stack-panels/">http://www.silverlight.net/learn/videos/all/using-silverlight-grids-and-stack-panels/</a> (also usable for learning about the StackPanel) </li> <li><a href="http://www.a2zdotnet.com/View.aspx?id=115">http://www.a2zdotnet.com/View.aspx?id=115</a> </li> </ul> </li> <li>The StackPanel: <br /> <em>A StackPanel can be used to place one or more of its children in a vertical (default) or horizontal layout. </em> <ul> <li>StackPanel at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel(v=VS.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel(v=VS.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-the-StackPanel-control-in-Silverlight-2-Beta-1.aspx">http://www.silverlightshow.net/items/Using-the-StackPanel-control-in-Silverlight-2-Beta-1.aspx</a> </li> <li><a href="http://www.dotnetfunda.com/articles/article876-using-silverlight-stackpanel-layout-control-.aspx">http://www.dotnetfunda.com/articles/article876-using-silverlight-stackpanel-layout-control-.aspx</a> </li> <li><a href="http://www.a2zdotnet.com/View.aspx?Id=116">http://www.a2zdotnet.com/View.aspx?Id=116</a> </li> </ul> </li> <li>The Canvas <br /> <em>A Canvas allows us to position items explicitly using a coordinate value. We can compare the Canvas with a Windows Forms on which we drop control and they get an absolute position.</em> <ul> <li>Canvas at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-the-Canvas-control-in-Silverlight-2-Beta-1.aspx">http://www.silverlightshow.net/items/Using-the-Canvas-control-in-Silverlight-2-Beta-1.aspx</a> </li> <li><a href="http://www.a2zdotnet.com/View.aspx?id=117">http://www.a2zdotnet.com/View.aspx?id=117</a> </li> <li><a href="http://www.silverlight.net/learn/videos/all/using-the-silverlight-canvas/">http://www.silverlight.net/learn/videos/all/using-the-silverlight-canvas/</a> (video) </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/anavijai/4439/Default.aspx">http://www.c-sharpcorner.com/UploadFile/anavijai/4439/Default.aspx</a> </li> <li>Canvas Z-index: <a href="http://www.vectorlight.net/silverlight/tutorials/rendering_order_using_z-index.aspx">http://www.vectorlight.net/silverlight/tutorials/rendering_order_using_z-index.aspx</a> </li> </ul> </li> <li>The Border: <br /> <em>A Border can be used to draw a border around a control and a background color can be applied on it.</em> <ul> <li>Border at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.border(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.border(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-the-Border-control-in-Silverlight-2-Beta-1-.aspx">http://www.silverlightshow.net/items/Using-the-Border-control-in-Silverlight-2-Beta-1-.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/raj1979/SilverlightBorder10162008030241AM/SilverlightBorder.aspx">http://www.c-sharpcorner.com/UploadFile/raj1979/SilverlightBorder10162008030241AM/SilverlightBorder.aspx</a> </li> </ul> </li> <li>The ScrollViewer: <br /> <em>A ScrollViewer can be used to scroll through content larger than the actual area.</em> <ul> <li>ScollViewer at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/tips/How-to-make-a-control-scroll-its-content.aspx">http://www.silverlightshow.net/tips/How-to-make-a-control-scroll-its-content.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mamta_m/307312009072507AM/3.aspx">http://www.c-sharpcorner.com/UploadFile/mamta_m/307312009072507AM/3.aspx</a> </li> <li><a href="http://csharperimage.jeremylikness.com/2010/05/making-scrollviewer-talk-in-silverlight.html">http://csharperimage.jeremylikness.com/2010/05/making-scrollviewer-talk-in-silverlight.html</a> </li> </ul> </li> <li>The ViewBox: <br /> <em>A ViewBox can be used to stretch a child to fill the available space.</em> <ul> <li>ViewBox at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/How-to-center-and-scale-Silverlight-applications-using-ViewBox-control-.aspx">http://www.silverlightshow.net/news/How-to-center-and-scale-Silverlight-applications-using-ViewBox-control-.aspx</a> </li> <li><a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-viewbox.aspx">http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-viewbox.aspx</a> </li> <li><a href="http://www.bluerosegames.com/SilverlightBrassTacks/post/Viewbox-in-the-Silverlight-Toolkit.aspx">http://www.bluerosegames.com/SilverlightBrassTacks/post/Viewbox-in-the-Silverlight-Toolkit.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/ViewBox03152009221804PM/ViewBox.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/ViewBox03152009221804PM/ViewBox.aspx</a> </li> </ul> </li> <li>Other related topics: <ul> <li>Controlling the Visibility: <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.visibility(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.uielement.visibility(v=vs.95).aspx</a> </li> <li>Controlling the Z-Index: <a href="http://stackoverflow.com/questions/1802108/how-can-we-change-zindex-of-a-silverlight-control-programmatically">http://stackoverflow.com/questions/1802108/how-can-we-change-zindex-of-a-silverlight-control-programmatically</a> </li> <li>Star-sizing: <a href="http://www.byteblocks.com/post/2010/04/27/Silverlight-Grid-Layout.aspx">http://www.byteblocks.com/post/2010/04/27/Silverlight-Grid-Layout.aspx</a> </li> <li>Positioning overview: <a href="http://msdn.microsoft.com/en-us/library/cc189087(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/cc189087(v=vs.95).aspx</a> </li> </ul> </li> </ul> <h2>Implement and configure core controls</h2> <p>Silverlight has an extensive list of controls that gets bigger with every release until now (Silverlight 1.0 had no controls!). Most controls are living in the assemblies that are installed by default with the plugin (sometimes referred to as core controls). Others such as the DataGrid and the Calendar control are part of a separate assembly that gets added to your project (and XAP file) only when used. The main goal of this process is keeping the initial plug-in download size as small as possible. When such a control gets added to the page, we need to reference the namespace as well. If we do this using the designer in Visual Studio, this is done automatically for us. </p> <p>Let’s look at the most common controls:</p> <ul> <li>The TextBox (inherits from System.Windows.Controls.Control): <br /> <em>A TextBox can be used to enter text by the user. It can display single-format, multi-line text.</em> <ul> <li>TextBox at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox(v=vs.95).aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTextBox12252008202141PM/SilverlightTextBox.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTextBox12252008202141PM/SilverlightTextBox.aspx</a> </li> </ul> </li> <li>The Button: <br /> <em>Represents well yeah, a Button! </em> <ul> <li>Button at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.button(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.button(v=vs.95).aspx</a> </li> <li>In this article, you can find more about other types of Buttons, including the HyperLinkButton and ToggleButton: <a href="http://www.silverlightshow.net/items/Button-Controls-in-Silverlight-2-Beta-1.aspx">http://www.silverlightshow.net/items/Button-Controls-in-Silverlight-2-Beta-1.aspx</a> </li> </ul> </li> <li>The TextBlock: <br /> <em>A TextBlock can be used to display small amounts of text.</em> <ul> <li>TextBlock at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock(v=vs.95).aspx</a> </li> <li><a href="http://blogs.silverlight.net/blogs/msnow/archive/2008/09/24/silverlight-tip-of-the-day-45-text-formatting-with-the-textblock-control.aspx">http://blogs.silverlight.net/blogs/msnow/archive/2008/09/24/silverlight-tip-of-the-day-45-text-formatting-with-the-textblock-control.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/TextTrimming-TextBlock-for-Silverlight-.aspx">http://www.silverlightshow.net/news/TextTrimming-TextBlock-for-Silverlight-.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTextBlock12282008230604PM/SilverlightTextBlock.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTextBlock12282008230604PM/SilverlightTextBlock.aspx</a> </li> <li><a href="http://www.dotnetfunda.com/tutorials/silverlight/text.aspx">http://www.dotnetfunda.com/tutorials/silverlight/text.aspx</a> </li> </ul> </li> <li>The CheckBox: <br /> <em>A CheckBox can be used to select or unselect an item.</em> <ul> <li>CheckBox at MSDN (contains list of all properties and methods): <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.checkbox(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.checkbox(v=vs.95).aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightCheckBox11282008214151PM/SilverlightCheckBox.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightCheckBox11282008214151PM/SilverlightCheckBox.aspx</a> </li> <li><a href="http://www.vectorlight.net/silverlight/controls/checkbox.aspx">http://www.vectorlight.net/silverlight/controls/checkbox.aspx</a> </li> </ul> </li> <li>Content controls: <br /> <em>A content control is a control that can contain any arbitrary content, such as another control. The ContentControl class is a base class that represents a control that can contain just one child. If more children are required, a container can be used a direct child.</em> <ul> <li>ContentControl class at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Tip-of-the-Day-Content-It-Ain-t-Just-Text.aspx">http://www.silverlightshow.net/items/Tip-of-the-Day-Content-It-Ain-t-Just-Text.aspx</a> </li> <li><a href="http://blogs.msdn.com/b/knowledgecast/archive/2008/07/14/writing-a-silverlight-content-control.aspx">http://blogs.msdn.com/b/knowledgecast/archive/2008/07/14/writing-a-silverlight-content-control.aspx</a> </li> <li><a href="http://channel9.msdn.com/Blogs/mtaulty/Silverlight-Content-Controls">http://channel9.msdn.com/Blogs/mtaulty/Silverlight-Content-Controls</a> </li> </ul> </li> <li>Referencing namespaces <br /> <em>When using controls that are is a namespace other than the default namespace (perhaps is a different assembly), namespaces need to be used inside XAML . Using these namespace, we can use 2 classes with the same name: assume we built our own Button class in some namespace, we can use both our own Button and the default Button. Using a namespace a a prefix for that namespace, we know which class we meant to use.</em> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/cc189061(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/cc189061(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/A-guide-to-cleaner-XAML-with-custom-namespaces-and-prefixes-WPF-Silverlight-.aspx">http://www.silverlightshow.net/news/A-guide-to-cleaner-XAML-with-custom-namespaces-and-prefixes-WPF-Silverlight-.aspx</a> </li> </ul> </li> </ul> <h2>Create user controls</h2> <p>Creating our own controls in Silverlight is simpler than in other technologies. On top of that, there are several ways of doing so. The first and probably the easiest way is a user control. Such a control is nothing more than a combination of some existing controls. This can then be reused throughout our application. We can define dependency properties on a user control, so it can be used in combination with data binding and styling. </p> <p>We can also define custom controls, where we override the control template. In such a case, we can use the Visual State Manager and the Parts and State Model. In this case, we define how the control should look in specific states. We’ll cover the VSM in part 2 of this series in more depth.</p> <p>Take a look at the following articles to get understanding of user controls:</p> <ul> <li>UserControl: <ul> <li>UserControl class at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Tutorial-Create-a-Silverlight-2-User-Control-from-a-Popup-Control.aspx">http://www.silverlightshow.net/items/Tutorial-Create-a-Silverlight-2-User-Control-from-a-Popup-Control.aspx</a> </li> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-6-using-user-controls-to-implement-master-detail-scenarios.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-6-using-user-controls-to-implement-master-detail-scenarios.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Silverlight-2-Custom-Controls-vs.-User-Controls.aspx">http://www.silverlightshow.net/items/Silverlight-2-Custom-Controls-vs.-User-Controls.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Creating-a-Skinnable-Custom-Control-in-Silverlight-3-Part-I.aspx">http://www.silverlightshow.net/items/Creating-a-Skinnable-Custom-Control-in-Silverlight-3-Part-I.aspx</a> </li> <li><a href="http://www.insidercoding.com/post/2008/07/14/Tutorial-Creating-a-User-Control-in-Silverlight-20.aspx">http://www.insidercoding.com/post/2008/07/14/Tutorial-Creating-a-User-Control-in-Silverlight-20.aspx</a> </li> </ul> </li> <li>Control parts: <ul> <li><a href="http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx">http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx</a> </li> <li><a href="http://blogs.msdn.com/b/sburke/archive/2008/03/22/tutorial-writing-a-templated-silverlight-2-control.aspx">http://blogs.msdn.com/b/sburke/archive/2008/03/22/tutorial-writing-a-templated-silverlight-2-control.aspx</a> </li> <li><a href="http://www.jeff.wilcox.name/2010/04/template-part-tips/">http://www.jeff.wilcox.name/2010/04/template-part-tips/</a> </li> <li><a href="http://marktinderholt.wordpress.com/2008/05/03/creating-a-custom-silverlight-control-the-dropdownlist/">http://marktinderholt.wordpress.com/2008/05/03/creating-a-custom-silverlight-control-the-dropdownlist/</a> </li> </ul> </li> </ul> <h2>Implement a navigation framework</h2> <p>Silverlight 2 didn’t have real navigation built-in. It was only with Silverlight 3 that the Navigation Framework made its appearance into Silverlight. This is based on the concept of a frame that shows pages and takes care of navigating from page to page: it will load a different page into the frame. Navigation can be triggered from XAML or from code. The latter is done using the NavigationService, which exposes a Navigate method that we can use to navigate to another page. In Silverlight 4, an extension point has been added in the form of the INavigationContentLoader. </p> <p>To learn more about navigation in Silverlight, take a look at the following links:</p> <ul> <li>In this webinar, Gill Cleeren talks about navigation in Silverlight: <a href="http://www.silverlightshow.net/news/Webinar-Build-Silverlight-Application.aspx">http://www.silverlightshow.net/news/Webinar-Build-Silverlight-Application.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/The-Silverlight-3-Navigation-Framework.aspx">http://www.silverlightshow.net/items/The-Silverlight-3-Navigation-Framework.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/What-s-New-In-Silverlight-3-Navigation-.aspx">http://www.silverlightshow.net/news/What-s-New-In-Silverlight-3-Navigation-.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/What-s-new-in-Silverlight-3.0-Navigation-Framework.aspx">http://www.silverlightshow.net/news/What-s-new-in-Silverlight-3.0-Navigation-Framework.aspx</a> </li> <li><a href="http://www.silverlight.net/learn/videos/all/navigation-framework/">http://www.silverlight.net/learn/videos/all/navigation-framework/</a> </li> <li><a href="http://www.silverlightshow.net/news/More-on-Silverlight-3-s-Navigation-Framework-.aspx">http://www.silverlightshow.net/news/More-on-Silverlight-3-s-Navigation-Framework-.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Silverlight-4-s-New-INavigationContentLoader-Interface-.aspx">http://www.silverlightshow.net/news/Silverlight-4-s-New-INavigationContentLoader-Interface-.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/View-Based-Navigation-in-Silverlight-3-.aspx">http://www.silverlightshow.net/news/View-Based-Navigation-in-Silverlight-3-.aspx</a> </li> <li>Page methods: <a href="http://msdn.microsoft.com/en-us/library/ms611629(v=VS.95).aspx">http://msdn.microsoft.com/en-us/library/ms611629(v=VS.95).aspx</a> (includes OnNavigatedFrom, OnNavigatedTo…) </li> <li><a href="http://www.silverlightshow.net/news/Deep-Linking-and-Browser-History-Navigation-in-Silverlight.aspx">http://www.silverlightshow.net/news/Deep-Linking-and-Browser-History-Navigation-in-Silverlight.aspx</a> </li> <li><a href="http://www.vsj.co.uk/articles/display.asp?id=790">http://www.vsj.co.uk/articles/display.asp?id=790</a> </li> <li><a href="http://timheuer.com/blog/archive/2009/03/22/silverlight-navigation-framework-and-uri-routing.aspx">http://timheuer.com/blog/archive/2009/03/22/silverlight-navigation-framework-and-uri-routing.aspx</a> </li> </ul> <h2>Display collections of items</h2> <p>Controls that display collections of items are indispensable. Silverlight includes the ComboBox, the ListBox, the TabControl and probably the most important one, the DataGrid. The latter can be used to display an Excel-like representation of data. The ItemsControl is a generic control for displaying lists of data. It can be compared to the ASP.NET repeater in some way. In many cases, these controls are templated. Using an item template, we can override how each individual item will be displayed.</p> <p>The following links will help you understand these controls.</p> <ul> <li>The ItemsControl <br /> <em>This control can be used to display a list of items. </em> <ul> <li>ItemsControl at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(VS.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(VS.95).aspx</a> </li> <li>Templating the ItemsControl: <a href="http://weblogs.asp.net/dwahlin/archive/2008/04/03/using-silverlight-2-itemscontrol-templates.aspx">http://weblogs.asp.net/dwahlin/archive/2008/04/03/using-silverlight-2-itemscontrol-templates.aspx</a> </li> </ul> </li> <li>The ListBox <br /> <em>The ListBox is used to display a scrollable list of items.</em> <ul> <li>ListBox at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox(v=vs.95).aspx</a> </li> <li><a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx">http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx</a> </li> <li><a href="http://www.vectorlight.net/silverlight/controls/listbox.aspx">http://www.vectorlight.net/silverlight/controls/listbox.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightListBox07212008115956AM/SilverlightListBox.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightListBox07212008115956AM/SilverlightListBox.aspx</a> </li> <li><a href="http://johnpapa.net/silverlight/customize-a-listbox-template-with-silverlight/">http://johnpapa.net/silverlight/customize-a-listbox-template-with-silverlight/</a> </li> </ul> </li> <li>The ComboBox <br /> <em>The ComboBox control displays a dropdown with items in which the user can make a selection.</em> <ul> <li>ComboBox at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox(v=vs.95).aspx</a> </li> <li><a href="http://johnpapa.net/silverlight/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath/">http://johnpapa.net/silverlight/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath/</a> </li> </ul> </li> <li>The TreeView: <br /> <em>The TreeView class shows a mostly hierarchical view of items.</em> <ul> <li>TreeView at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.treeview(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.treeview(v=vs.95).aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Using-the-TreeView-control-in-Silverlight-2.aspx">http://www.silverlightshow.net/items/Using-the-TreeView-control-in-Silverlight-2.aspx</a> </li> <li> <a href="http://www.silverlightshow.net/items/Silverlight-Tree-View-Control.aspx">http://www.silverlightshow.net/items/Silverlight-Tree-View-Control.aspx</a> </li> <li> <a href="http://blogs.silverlight.net/blogs/justinangel/archive/2009/05/19/silverlight-treeview-advanced-scenarios-treeviewextended.aspx">http://blogs.silverlight.net/blogs/justinangel/archive/2009/05/19/silverlight-treeview-advanced-scenarios-treeviewextended.aspx</a> </li> <li><a href="http://www.vectorlight.net/silverlight/demos/treeview.aspx">http://www.vectorlight.net/silverlight/demos/treeview.aspx</a> </li> </ul> </li> <li>The DataGrid: <br /> <em>The DataGrid can be used to show items in a table-like format. The DataGrid was enhanced in Silverlight 4: new sizing features were added.</em> <ul> <li>DataGrid at MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.95).aspx</a> </li> <li><a href="http://blogs.msdn.com/b/scmorris/archive/2008/03/21/using-the-silverlight-datagrid.aspx">http://blogs.msdn.com/b/scmorris/archive/2008/03/21/using-the-silverlight-datagrid.aspx</a> </li> <li><a href="http://www.c-sharpcorner.com/UploadFile/mahesh/SLDataGrid05032009223331PM/SLDataGrid.aspx">http://www.c-sharpcorner.com/UploadFile/mahesh/SLDataGrid05032009223331PM/SLDataGrid.aspx</a> </li> <li><a href="http://www.silverlightshow.net/news/Grouping-in-Silverlight-3-DataGrid-.aspx">http://www.silverlightshow.net/news/Grouping-in-Silverlight-3-DataGrid-.aspx</a> </li> <li><a href="http://www.silverlightshow.net/items/Defining-Columns-for-a-Silverlight-DataGrid.aspx">http://www.silverlightshow.net/items/Defining-Columns-for-a-Silverlight-DataGrid.aspx</a> </li> <li><a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-data-grid-enhancements.aspx">http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-data-grid-enhancements.aspx</a> </li> <li><a href="http://www.dotnetspider.com/resources/32211-Conditional-formatting-Datagrid.aspx">http://www.dotnetspider.com/resources/32211-Conditional-formatting-Datagrid.aspx</a> </li> <li><a href="http://weblogs.asp.net/brijmohan/archive/2010/05/14/working-with-silverlight-datagrid-rowdetailstemplate.aspx">http://weblogs.asp.net/brijmohan/archive/2010/05/14/working-with-silverlight-datagrid-rowdetailstemplate.aspx</a> </li> </ul> </li> </ul> <h2>Play media files</h2> <p>Silverlight can since its first appearance play media (audio and video) very well. It includes support for HD content and supports DRM (which is good news for media companies, but less good news for all the rest of us!). The following links can help you understanding the media capabilities of Silverlight:</p> <ul> <li>The MediaElement: <a href="http://www.c-sharpcorner.com/UploadFile/mamta_m/111292008005520AM/1.aspx">http://www.c-sharpcorner.com/UploadFile/mamta_m/111292008005520AM/1.aspx</a> </li> <li>DRM in Silverlight: <a href="http://msdn.microsoft.com/en-us/library/cc838192(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/cc838192(v=vs.95).aspx</a> </li> <li>Streaming content: <a href="http://www.learn-silverlight-tutorial.com/StreamingMediaUsingSilverlight.cfm">http://www.learn-silverlight-tutorial.com/StreamingMediaUsingSilverlight.cfm</a> </li> <li>Playing multiple audio tracks: <a href="http://10rem.net/blog/2009/07/19/silverlight-synthesizer-source-code-%E2%80%93-mediastreamsource-raw-sound">http://10rem.net/blog/2009/07/19/silverlight-synthesizer-source-code-–-mediastreamsource-raw-sound</a> </li> <li>Timeline marker events: <a href="http://openlightgroup.net/Blog/tabid/58/EntryId/65/Closed-Captioning-with-Silverlight-using-MVVM.aspx">http://openlightgroup.net/Blog/tabid/58/EntryId/65/Closed-Captioning-with-Silverlight-using-MVVM.aspx</a> </li> <li>Playback events: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement_events(v=vs.95).aspx">http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement_events(v=vs.95).aspx</a> </li> </ul> <h1>Conclusion</h1> <p>After this long list of links, I hope you have a now deeper understanding of the topics we looked at. You should now have a solid foundation to build on!</p> <p>Stay tuned for part 2 where we’ll look at preparing for the Enhancing the User Interface part of the exam!</p> http://www.silverlightshow.net/items/Getting-ready-for-the-exams-Part-1.aspx editorial@silverlightshow.net (Gill Cleeren ) http://www.silverlightshow.net/items/Getting-ready-for-the-exams-Part-1.aspx#comments http://www.silverlightshow.net/items/Getting-ready-for-the-exams-Part-1.aspx Mon, 17 Jan 2011 01:38:00 GMT Working with the Silverlight Rich Text Box control <p><em><strong>This article is compatible with the latest version of Silverlight.</strong></em></p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>Don't miss...</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Webinars.aspx">Free SilverlightShow Webinars</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/Search.aspx?q=+RichTextBox&adv=false">See all posts related to RichTextBox</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx">Pro Business Applications with SL4 book: </a></li> </ul> <p style="padding-bottom: 5px;">        <a href="http://www.silverlightshow.net/book/Pro-Business-Applications-with-Silverlight-3.aspx"> <img alt="Pro Business Applications with Silverlight 4 book" src="http://www.silverlightshow.net/Storage/ProSL4.jpg" /></a> </p> <p style="font-size: 12px;">           <a href="http://www.silverlightshow.net/Books.aspx">Show more books</a><img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>In this article, we’ll take a look at the Silverlight Rich Text Box control. The Rich Text Box was one of the new, and highly requested controls introduced in Silverlight 4.</p> <p>This article is accompanied by a Visual Studio solution, which you can find <a href="http://www.silverlightshow.net/Storage/Sources/SilverlightShowRichTextBox.zip" target="_blank">here</a>. </p> <h3> </h3> <h3>What do we get?</h3> <p>Out of the box, the Rich Text Box control looks like a simple TextBox: you get the typical TextBox look, nothing more. However, under the covers, it offers us the ability to display formatted text, paragraphs, hyperlinks or inline images – and even more than that: you can actually use the Rich Text Box to display any UIElement (so if you want a DataGrid in your Rich Text Box, that’s entirely possible).</p> <p>The Rich Text Box has a content property, Blocks, which is a collection of Paragraph elements (Paragraph derives from Block). These Paragraph elements can in turn contain elements that are derived from Inline, like Run (we know that one from the regular Text Box), Span, Bold, Italic, Underline, Hyperlink and the InlineUIContainer (which can contain UIElements).</p> <p>This image (from MSDN) offers an overview of the content model: </p> <p><a href="http://www.silverlightshow.net/Storage/Users/KevinDockx/_image_2.png"><img width="497" height="329" title="image" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/Users/KevinDockx/_image_thumb.png" /></a></p> <p>As you can see in this image, Inline elements (excluding Hyperlink) can contain other Inline elements. Looking at the above image, you can already guess we’re dealing with a really powerful control.</p> <p>We’ll run through each of these possibilities in this article, and we’ll also look at a few more options the Rich Text Box offers.  We’ll end up with a Silverlight project looking like this:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/KevinDockx/_image_4.png"><img width="638" height="424" title="image" style="border:0px; background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/Users/KevinDockx/_image_thumb_1.png" /></a></p> <p>But let’s start by adding the control itself to our view. </p> <h3>Adding a Rich Text Box control</h3> <p>As with all XAML controls, this is pretty simple: the Rich Text Box control is contained in the System.Windows.Controls namespace, in the System.Windows assembly. To use it, simply add the following XAML syntax to your View (no imports are needed):</p> <pre class="csharpcode"><span class="kwrd"><</span><span class="html">RichTextBox</span> <span class="attr">AcceptsReturn</span><span class="kwrd">="True"</span> <span class="attr">Margin</span><span class="kwrd">="5"</span> <span class="attr">x:Name</span><span class="kwrd">="rbtMyRichTextBox"</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">RichTextBox</span><span class="kwrd">></span></pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>Easy enough. The two most commonly used properties of the Rich Text Box are probably AcceptReturn (we’ve put this to True) and IsReadOnly. It’s only in read-only mode that UI elements and Hyperlinks are active (you can click the Hyperlink or actively use the other UI elements). </p> <h3>Adding elements</h3> <p>As a Rich Text Box consists of Paragraphs, which consists of Inline elements, we can easily add each and any type of Inline element we like. First, we’ll add a few buttons to our UI to add content to the control. In the button handlers, we’ll create the content. In the included source code for this article, you can find code to add various kinds of elements, and we’ll have a look at combining a few elements into one Paragraph element.</p> <p>To add some text with a hyperlink, what we need to do is create a new Paragraph. To this Paragraph, we’ll add the other elements we need: the plain text, and the Hyperlink. These objects can be added to the Inlines collection of the Paragraph. In the end, we add the Paragraph to the Blocks collection of the Rich Text Box:</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> AddTextAndLink_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { <span class="rem">// create a paragraph</span> Paragraph prgParagraph = <span class="kwrd">new</span> Paragraph(); <span class="rem">// create some text, and add it to the paragraph</span> Run rnMyText = <span class="kwrd">new</span> Run(); rnMyText.Text = <span class="str">"This is some example text with a "</span>; prgParagraph.Inlines.Add(rnMyText); <span class="rem">// create a link, and add it</span> Hyperlink lnkSSLink = <span class="kwrd">new</span> Hyperlink(); lnkSSLink.Inlines.Add(<span class="str">"link to Silverlight Show"</span>); lnkSSLink.NavigateUri = <span class="kwrd">new</span> Uri(<span class="str">"http://www.silverlightshow.net"</span>); prgParagraph.Inlines.Add(lnkSSLink); <span class="rem">// add the paragraph to the RTB</span> rbtMyRichTextBox.Blocks.Add(prgParagraph); }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>Run can only contain unformatted text. To format this text, we can include the Run element in the Inline collection of one of the “format” Inlines, like Bold, Italic, … All elements in the Inline collection of, for example, Bold, will be formatted with a heavier weight. And what’s more: these elements can be combined, so we can create bold, italic text:</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> AddFormattedText_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { <span class="rem">// create a paragraph</span> Paragraph prgParagraph = <span class="kwrd">new</span> Paragraph(); <span class="rem">// create some text, and add it to the paragraph</span> Bold bldText = <span class="kwrd">new</span> Bold(); bldText.Inlines.Add(<span class="kwrd">new</span> Run() { Text = <span class="str">"This is some example text in bold"</span> }); Italic itlText = <span class="kwrd">new</span> Italic(); itlText.Inlines.Add(<span class="kwrd">new</span> Run() { Text = <span class="str">"This is some example text in italics"</span> }); Underline unText = <span class="kwrd">new</span> Underline(); unText.Inlines.Add(<span class="kwrd">new</span> Run() { Text = <span class="str">"This is some example text, underlined"</span> }); Bold bldTextWithItalic = <span class="kwrd">new</span> Bold(); bldTextWithItalic.Inlines.Add(<span class="kwrd">new</span> Italic() { Inlines = { <span class="kwrd">new</span> Run() <br />    { Text = <span class="str">"This is some example text, bold and italic"</span> } } }); prgParagraph.Inlines.Add(bldText); prgParagraph.Inlines.Add(<span class="kwrd">new</span> LineBreak()); prgParagraph.Inlines.Add(itlText); prgParagraph.Inlines.Add(<span class="kwrd">new</span> LineBreak()); prgParagraph.Inlines.Add(unText); prgParagraph.Inlines.Add(<span class="kwrd">new</span> LineBreak()); prgParagraph.Inlines.Add(bldTextWithItalic); rbtMyRichTextBox.Blocks.Add(prgParagraph); }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> But what if you want to add, say, an Image or even a DataGrid to your Rich Text Box? To this avail, the InlineUIContainer exists, which acts as a container for any UIElement. The same principles apply as with other Inline elements: create an InlineUIContainer, add your UIElement to it, add the container to a Paragraph, and at the Paragraph to the RichTextBox: <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> AddDataGrid_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { InlineUIContainer iuicContainer = <span class="kwrd">new</span> InlineUIContainer(); DataGrid dtgGrid = <span class="kwrd">new</span> DataGrid(); dtgGrid.AutoGenerateColumns = <span class="kwrd">true</span>; dtgGrid.Width = 400; dtgGrid.Height = 200; dtgGrid.ItemsSource = CreatePersonList(); iuicContainer.Child = dtgGrid; Paragraph prgParagraph = <span class="kwrd">new</span> Paragraph(); prgParagraph.Inlines.Add(iuicContainer); rbtMyRichTextBox.Blocks.Add(prgParagraph); }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>Of course, all these elements can be added through XAML instead of through code. For example, adding an image to our Rich Text Box with XAML code could be achieved like this:</p> <pre class="csharpcode"><span class="kwrd"><</span><span class="html">RichTextBox</span> <span class="attr">AcceptsReturn</span><span class="kwrd">="True"</span> <span class="attr">Margin</span><span class="kwrd">="5"</span> <span class="attr">x:Name</span><span class="kwrd">="rbtMyRichTextBox"</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">Paragraph</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">InlineUIContainer</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">Image</span> <span class="attr">Source</span><span class="kwrd">="Assets/logo.png"</span> <span class="attr">Width</span><span class="kwrd">="100"</span><span class="kwrd">></</span><span class="html">Image</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">InlineUIContainer</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">Paragraph</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">RichTextBox</span><span class="kwrd">></span></pre> <h3>What about manipulating a selection?</h3> <p>Up until now, we’ve programmatically added elements to our Rich Text Box control (and of course you can just type in it), but this is not the typical behavior a user expects from a control like this: he wants to be able to select text and manipulate it any way he wants (eg: making the text bold). Selecting text is possible out of the box, and the Rich Text Box control has a Selection property of type TextSelection, which contains the currently selected text. We can manipulate this selection through the GetPropertyValue and ApplyPropertyValue methods: we get the FontWeight property value, check if it’s normal or bold, and change the value accordingly.</p> <p>Afterwards, we put the focus back on the Rich Text Box.</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> MakeBold_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { <span class="rem">// change text</span> <span class="kwrd">if</span> (rbtMyRichTextBox != <span class="kwrd">null</span> && rbtMyRichTextBox.Selection.Text.Length > 0) { <span class="kwrd">if</span> (rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty) <span class="kwrd">is</span> FontWeight && ((FontWeight)rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty)) </pre> <pre class="csharpcode">== FontWeights.Normal) { rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold); } <span class="kwrd">else</span> { rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal); } } <span class="rem">// return focus</span> <span class="kwrd">if</span> (rbtMyRichTextBox != <span class="kwrd">null</span>) { rbtMyRichTextBox.Focus(); } }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>Important to know is that you can only change property values like this if your selection extends over text with only one value for the formatting property – if not, UnsetValue is returned.</p> <p>By applying the same logic, you can manipulate other text properties, like font size, font family, … </p> <h3>I want to get some copy & paste functionality.</h3> <p>What’s a Rich Text Box control without copy and paste functionality? It’s something users expect, and thanks to the fact that we can access the clipboard from Silverlight (through System.Windows.Clipboard), it’s pretty easy to implement: the selected text is in the Selection property of the Rich Text Box, and can be added to the clipboard as such (for copy and cut):</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> Copy_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { Clipboard.SetText(rbtMyRichTextBox.Selection.Text); rbtMyRichTextBox.Focus(); } <span class="kwrd">private</span> <span class="kwrd">void</span> Cut_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { Clipboard.SetText(rbtMyRichTextBox.Selection.Text); rbtMyRichTextBox.Selection.Text = <span class="str">""</span>; rbtMyRichTextBox.Focus(); }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>To paste our text, we should just set the Selection property of the Rich Text Box to the text from the clipboard. This will either overwrite the selection, or, if nothing is selected, insert the text at the position of the cursor:</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> Paste_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { rbtMyRichTextBox.Selection.Text = Clipboard.GetText(); rbtMyRichTextBox.Focus(); }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>After each operation, we should of course return the focus to the Rich Text Box control.</p> <p>Important to know is that you will lose formatting when using copy/paste, because the Silverlight Clipboard can only contain simple text. </p> <h3>Ok, so I’ve created a document. How do I save it?</h3> <p>The easiest way to save the contents of a Rich Text Box is by persisting the underlying XAML syntax of the Paragraph, Inline, … elements to a file. The Rich Text Box has a property through which you can easily access the underlying XAML code: Xaml.</p> <p>What we do not want to save are the InlineUIContainer elements as this is not supported, so we check for this. If there are none, we open a Save File Dialog, and use a FileStream to write the XAML to the file:</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> Save_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { <span class="rem">// we do not save UI Elements</span> var uiElements = from block <span class="kwrd">in</span> rbtMyRichTextBox.Blocks from inline <span class="kwrd">in</span> (block <span class="kwrd">as</span> Paragraph).Inlines <span class="kwrd">where</span> inline.GetType() == <span class="kwrd">typeof</span>(InlineUIContainer) select inline; <span class="kwrd">if</span> (uiElements.Count() != 0) { MessageBox.Show(<span class="str">"UIElements cannot be saved"</span>); <span class="kwrd">return</span>; } SaveFileDialog sfdSaveXaml = <span class="kwrd">new</span> SaveFileDialog(); sfdSaveXaml.DefaultExt = <span class="str">".sav"</span>; sfdSaveXaml.Filter = <span class="str">"Saved rtb files|*.rtb"</span>; <span class="kwrd">if</span> (sfdSaveXaml.ShowDialog().Value) { <span class="kwrd">using</span> (FileStream fs = (FileStream)sfdSaveXaml.OpenFile()) { System.Text.UTF8Encoding enc = <span class="kwrd">new</span> System.Text.UTF8Encoding(); <span class="rem">// the Xaml is what we want to save</span> <span class="kwrd">byte</span>[] buffer = enc.GetBytes(rbtMyRichTextBox.Xaml); fs.Write(buffer, 0, buffer.Length); fs.Close(); } } }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <p>Opening a file is easy as well: just read all the XAML back and set the .Xaml property of your Rich Text Box to this code.</p> <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> Open_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e) { <span class="rem">// open file</span> OpenFileDialog ofdOpenXaml = <span class="kwrd">new</span> OpenFileDialog(); ofdOpenXaml.Multiselect = <span class="kwrd">false</span>; ofdOpenXaml.Filter = <span class="str">"Saved rtb files|*.rtb"</span>; <span class="kwrd">if</span> (ofdOpenXaml.ShowDialog().Value) { FileInfo fiXamlFile = ofdOpenXaml.File; StreamReader sr = fiXamlFile.OpenText(); rbtMyRichTextBox.Xaml = sr.ReadToEnd(); sr.Close(); } }</pre> <style type="text/css"> .csharpcode { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small } .csharpcode PRE { MARGIN: 0em } .csharpcode .rem { COLOR: #008000 } .csharpcode .kwrd { COLOR: #0000ff } .csharpcode .str { COLOR: #006080 } .csharpcode .op { COLOR: #0000c0 } .csharpcode .preproc { COLOR: #cc6633 } .csharpcode .asp { BACKGROUND-COLOR: #ffff00 } .csharpcode .html { COLOR: #800000 } .csharpcode .attr { COLOR: #ff0000 } .csharpcode .alt { BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100% } .csharpcode .lnum { COLOR: #606060 } </style> <h3>Can we save to other file formats, and open them as well?</h3> <p>The short answer is: yes, you can. The long answer: yes, you can, but it will require some extra coding on your part. The Rich Text Box expects XAML code, so if you want to open, say, a txt file, you’ll have to parse the text file yourself and create the necessary Run elements . The same logic applies for opening a basic xml file, a docx file, … you can do this, but: you’ll have to write functions to parse the contents of the file and convert it to XAML, which can then be applied to the Rich Text Box.</p> <p>Saving the file comes down to getting the XAML from your Rich Text Box, and converting it to the file format you want to save it to. You could look at the source code of the <a href="http://wordtoxaml.codeplex.com/">Word to XAML</a> project over at <a href="http://www.codeplex.com/">Codeplex</a> (this is a Word plugin, but you can get some inspiration on parsing docx content from it; there are others as well, both open source and commercial) to get an idea of how to start with this – but keep in mind that the more complex the file format you want to save to is, the more work it will cost you. A docx probably isn’t the best file format to start with, as they can quickly become quite complex. </p> <h3>Conclusion</h3> <p>In this article, we’ve looked into the possibilities of the Silverlight Rich Text Box control. We’ve learned what types of content the Rich Text Box control can contain, how we can add them and how we can manipulate them. We’ve also looked into adding the ability to copy and paste text, and finally enabled loading and saving our edited files.</p> <p> </p> <h3>About the author</h3> <p>Kevin Dockx lives in Belgium and works at <a href="http://www.realdolmen.com/">RealDolmen</a>, one of Belgium's biggest ICT companies, where he is a technical specialist/project leader on .NET web applications, mainly Silverlight, and a solution manager for Rich Applications (Silverlight, Windows Phone 7 Series, WPF, Surface). His main focus lies on all things Silverlight, but he still keeps an eye on the new developments concerning other products from the Microsoft .NET (Web) Stack. As a Silverlight enthusiast, he's a regular speaker on various national and international events, like Microsoft DevDays in The Netherlands, Microsoft Techdays in Portugal or on BESUG events (the Belgian Silverlight User Group). Next to that, he also authored a best-selling Silverlight book, <a href="https://www.packtpub.com/microsoft-silverlight-4-data-and-services-cookbook/book">Packt Publishing's Silverlight 4 Data and Services Cookbook</a>, together with <a href="http://www.snowball.be/">Gill Cleeren</a>. His blog, which contains various tidbits on Silverlight, .NET, and the occasional rambling, can be found at <a href="http://blog.kevindockx.com/">http://blog.kevindockx.com/</a>.</p> http://www.silverlightshow.net/items/Working-with-the-Silverlight-Rich-Text-Box-control.aspx editorial@silverlightshow.net (Kevin Dockx ) http://www.silverlightshow.net/items/Working-with-the-Silverlight-Rich-Text-Box-control.aspx#comments http://www.silverlightshow.net/items/Working-with-the-Silverlight-Rich-Text-Box-control.aspx Tue, 14 Dec 2010 11:36:00 GMT Step-by-Step Guide to Silverlight Shape Controls <p><em><span style="line-height: 115%; font-family: calibri, sans-serif; font-size: 11pt;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></p> <p>In my previous post “<a href="http://www.kunal-chowdhury.com/2010/09/introduction-to-shapes-in-silverlight-4.html" target="_blank">Introduction to Shapes in Silverlight</a>” I introduced you with various types of Shapes available in Silverlight and informed you that, I will post an article describing each one of them and here it is.</p> <p>In this article, I am going to describe you about each shapes and the process to add them in our application (whenever you need). This article is for the Beginners, who don’t know much about the shape controls. If you have any suggestions/concerns please use the feedback section to let me know.</p> <h2>Prerequisite</h2> <p>As mentioned in my previous post, you need “<span style="color: #800000;">Microsoft.Expression.Drawing.dll</span>” assembly to get the new Shape controls in hand to work with. You can get this dll once you install the Expression Blend SDK to your development PC. If you don’t have the Expression Blend, you can easily download it from the <a href="http://expression.microsoft.com/en-us/cc136530.aspx" target="_blank">Microsoft Expression site</a>.</p> <p>Apart from this, your development environment should have the following Tools already installed:</p> <ul> <li>Microsoft Visual Studio </li> <li>Silverlight Tools for Visual Studio </li> </ul> <h2>Introducing the Shape Controls</h2> <p>Once your development environment is ready, you can open the Expression Blend IDE and create a new Silverlight Application Project. I am demonstrating with the help of Expression Blend here, so that, it will be easy for you to understand properly.</p> <p><img width="400" height="342" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image8.png?imgmax=800" /></p> <p>Now, once your project has been created by the IDE, open your “Assets” tab and click on the “Shapes” sub section to expand it at the right. In the right pane you will see all the collection of the shapes available in Silverlight 4. </p> <p><img width="373" height="547" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image11.png?imgmax=800" /></p> <p>Lets start describing one by one. We will discuss both from the IDE and the XAML code. It will be helpful for you to understand. </p> <p>To work with the Shape controls, you need to add one xmlns namespace in your XAML page:</p> <p><img width="640" height="94" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image24.png?imgmax=800" /></p> <p>If you are using the Expression Blend IDE (designer), it will add this xmlns namespace automatically when you add the first shape control in your XAML page.</p> <h2>Arc Control</h2> <p>Let’s start with the Arc control. Earlier to Silverlight 4, how can you create an Arc shape? You have to use one Ellipse and two Rectangle controls to design the shape. You have to crop those controls as a Path control. Need more designing knowledge using the Blend IDE. Just think to create it from the backend code. OMG!!! You have to design the UserControl first as described earlier and the create the instance from the code behind and rotate it as necessary. Let us create a Arc using the Arc shape control. Click on the Arc symbol from the Assets pane and draw it in your designer page. </p> <p><img width="640" height="296" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image18.png?imgmax=800" /></p> <p>This will create the below XAML code:</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f04b5861-a01f-42f5-bdb7-b721c4bf1460" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 629px; height: 72px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage161.png?imgmax=800" /></div> <p>So easy right? Yes, it is very easy to design. Now, from the Properties panel, you can change any attribute of the shape like “StrokeThickness”, “ArcThickness”, “ArcThicknessUnit”, “StartAngel”, “EndAngel” etc.</p> <p>Setting the “StrokeThickness” will change the thickness of the Arc. Have look into the following snapshot:</p> <p><img width="640" height="300" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image27.png?imgmax=800" /></p> <p>Increasing the “ArcThickness” will change the Arc like this: </p> <p><img width="640" height="300" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image30.png?imgmax=800" /></p> <p>“StartAngel” and “EndAngel” describes the start and end point of the Arc. Here I used zero as the start angel and 90 as the end angel. Hence, it produced 90 degree Arc. If you set StartAngel as zero and EndAngel as 360, it will create a circle for you. The Ring control available in the list produces same code. </p> <p><img width="640" height="293" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B3%5D.png?imgmax=800" /></p> <p>If you want to draw a Pie in your UI, you can use the Pie shape control available in the list. It internally uses the same Arc control having different StartPoint and EndPoint to produce the Pie Shape. Have a look into the following snapshot: </p> <p><img width="640" height="298" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B6%5D.png?imgmax=800" /></p> <p>Setting the StartAngel to 90 and EndAngel to 360 creates the Pie Shape. You can adjust the angels to generate a different pie shape. </p> <h2>Block Arrow Control</h2> <p>If you want to add some arrows in your application for direction, what will you do if you are using the earlier version of Silverlight? Yes, you have to create an Arrow UserControl properly shaped with other controls like Rectangle and/or Paths. Then based on the direction you had to rotate the control. So Irritating!!!</p> <p>No need to tense more. Silverlight 4 is there for you. The Expression Blend SDK now has support for Block Arrow controls. There are four types of Block Arrow Control named “Block Arrow Down”, “Block Arrow Left”, “Block Arrow Top” and “Block Arrow Right”. They are not different controls, only changing the enum property will do the trick for you. So, no need to use transform to rotate the control and no need to use four different controls.</p> <p>From the Assets pane chose any one of the Block Arrow and draw four shapes of the same. Now, from the properties panel, search for the orientation property. Select “Left”, “Right”, “Up” or “Down” from the orientation dropdown based on your requirement. That’s it. It will change the shape accordingly.</p> <p><img width="640" height="295" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image36.png?imgmax=800" /></p> <p>There are some other common properties available for the Block Arrow control e.g. ArrowheadAngle and ArrowBodySize. They are responsible for changing the size of the arrow head and arrow body respectively. You can find those properties in the property panel. </p> <p><img width="271" height="231" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image39.png?imgmax=800" /></p> <p>Let’s see the XAML code, which has been generated for our four controls. Here it is: </p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:809148d2-75b2-4d6c-a6f7-ef68f01cfe16" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 631px; height: 298px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage330.png?imgmax=800" /></div> <p>As you see, the only change between them is the Orientation property. So, just setting the Enum value you can set the direction of the Block Arrow. <img class="wlEmoticon wlEmoticon-openmouthedsmile" style="border-style: none;" alt="Open-mouthed smile" src="http://lh4.ggpht.com/_MOj9mkIt81A/TIplVpMH_uI/AAAAAAAAGj0/riljZrojWAc/wlEmoticonopenmouthedsmile2.png?imgmax=800" /></p> <h2>Callout Control</h2> <p>Before starting with this point think 10 times, if you want to develop a Callout control in earlier version of Silverlight, how would you do this? Any idea? Hmmm… little bit difficult. Some hint is to create a vector image and scale it as per need or use some path controls to create the shape. Not a good idea but this needs some effort from a good designer. Am I right? Yes.</p> <p>But now Silverlight gives you those controls inbuilt in the runtime. Choice is yours, how do you want to utilize them. Like the Block Arrow, the Callout control also has four different enum types which you have to set based on your requirement.</p> <p>Here are some example of Callout Controls:</p> <p><img width="570" height="154" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image52.png?imgmax=800" /></p> <p>Once you draw a callout control in the XAML designer, in the properties panel you will see a property called “CalloutStyle”. This dropdown has four enum values called “Cloud”, “Oval”, “Rectangle” and “RoundedRectangle”, which ones set will produce the respective shape in your UI.</p> <p><img width="264" height="228" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image55.png?imgmax=800" /></p> <p>The property called “AnchorPoint” gives you the option to change the callout start point for the anchor. This displays as (x, y) coordinate value. The default value for the AnchorPoint is (0, 1.5). </p> <p>Here is the actual XAML code to produce four different types of callout shapes:</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:43fe53ed-5236-459a-a5d1-50b1e199ee54" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 631px; height: 411px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage113.png?imgmax=800" /></div> <p>You can easily find out the difference between each callout type.</p> <h2>Ellipse Control</h2> <p>This is not a new control in Silverlight 4. It was part of initial version of Silverlight too. You can find it in the System.Windows.dll assembly. Using Ellipse control, you can draw ellipse and circles.</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:bcaadd82-c4eb-42ea-8049-c6f70fc396c8" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 631px; height: 60px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage41.png?imgmax=800" /></div> <p>The above code will produce a circle as shown in the below screenshot:</p> <p><img width="300" height="225" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px solid;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image4.png?imgmax=800" /></p> <p>Setting different height & width, you can create an ellipse for your UI. Not going to discuss more on this control as it’s a common control for all.</p> <h2>Regular Polygon Control</h2> <p>Pentagon, Hexagon, Star and Triangle shape controls use the RegularPolygon internally to generate the said shapes in the UI. The properties called “InnerRadius” and “PointCount” are responsible to generate not only those shapes but other various shapes too. Have a look here:</p> <p><img width="640" height="293" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B9%5D.png?imgmax=800" /></p> <p>InnerRadius of 100% and PointCount of 6 produces a Hexagon Shape as shown above. The XAML code for the above shape will look as below:</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ff338a00-e419-4298-b35e-b50588d38201" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 630px; height: 67px;border: 0px;" src="http://lh6.ggpht.com/_MOj9mkIt81A/TIplcdGOJII/AAAAAAAAGkQ/pewYpIeres4/transformedimage%5B52%5D.png?imgmax=800" /></div> <p><img width="640" height="291" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B12%5D.png?imgmax=800" /></p> <p>Inner Radius of 100% and PointCount of 5 generates Pentagon as shown in the above figure.</p> <p><img width="640" height="291" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B18%5D.png?imgmax=800" /></p> <p>If you want to create a Star shape, set the InnerRadius to a lower value. Setting InnerRadius to 50% and PointCount to 5 generates a perfect Star shape. </p> <p><img width="640" height="296" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B15%5D.png?imgmax=800" /></p> <p>Want to add a Triangle in your page? Set the PointCount to 3. 3 is the minimum value for the PointCount property of the RegularPolygon shape control. </p> <p><img width="640" height="289" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B24%5D.png?imgmax=800" /></p> <p>You can also generate a circle using the same RegularPolygon shape control. Set the PointCount to 100 and it will create the above shape. Remember that, 100 is the Maximum value for the PointCount property. </p> <p><img width="640" height="295" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B21%5D.png?imgmax=800" /></p> <p>Setting the InnerRadius to 50% and PointCount to 100 will produce the above shape.</p> <h2>Rectangle Control</h2> <p>The Rectangle shape was available in earlier versions too. But, let me describe a bit of it here too. It is available as part of the System.Windows.dll assembly. The default generates a Rectangle shape. But tweaking it with the help of exposed properties, you can generate Rounded Rectangle or even a Circle too.</p> <p><img width="640" height="292" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B30%5D.png?imgmax=800" /></p> <p>In the default case, the RadiusX and RadiusY properties will have a value ‘0’ (zero) and hence it generates a pure square/rectangle shape.</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:28bdcdd8-77c7-44e8-abe6-91777ec3d768" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 630px; height: 54px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage%5B79%5D.png?imgmax=800" /></div> <p><img width="640" height="289" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B33%5D.png?imgmax=800" /></p> <p>Setting those properties higher than zero will render the rectangle with rounded corner. In the above screenshot, it has a value of 20 for both RadiusX & RadiusY. Hence, created a rounded corner. If you set them to a more higher value, it may change to a Circle too.</p> <h2>Line Arrow Control</h2> <p>It’s a new shape control introduced in Silverlight 4. Before going into the deep discussion, think of creating a simple arrow (straight or bendable). To create such kind of arrow will take a good effort from your production development. Now it comes with Silverlight runtime. Tweaking the control by setting proper properties you can make it straight or bended. You can also make it one directional or bi directional. Have a look here:</p> <p><img width="640" height="289" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B36%5D.png?imgmax=800" /></p> <p>Here is the XAML code:</p> <div class="wlWriterEditableSmartContent" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fb10f13d-61bc-4739-9c8d-e1370b5c7435" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px;"><img alt="" style="width: 628px; height: 56px;border: 0px;" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/transformedimage%5B111%5D.png?imgmax=800" /></div> <p>Setting the BendAmount to zero will create a straight line for you. You can also change the starting point by changing the StartCorner property of the control.</p> <p><img width="640" height="290" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B39%5D.png?imgmax=800" /></p> <p>If you want a fully bended arrow, you can do it by setting the BendAmount property to 1. Remember that, 1 is the highest value for the BendAmount. Have a look into the below screenshot:</p> <p><img width="640" height="289" title="image" style="background-image: none; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; padding-top: 0px;border: 0px;" alt="image" src="http://www.silverlightshow.net/Storage/userfiles/StepByStepGuideToShapeControls/image%5B42%5D.png?imgmax=800" /></p> <p>There are 5 types of ArrowHead. You can set them as the StartArrow and EndArrow property. See the above figure for more details. In the above example I set the StartArrow as “OvalArrow” and the EndArrow as the default “Arrow”.</p> <h2>End Note</h2> <p>This is all about the new Shape controls. Hope this long article will help you understanding the basics about them. Please note that, all the diagrams were drawn on top of a Grid control and hence had the property Margin set for each one of them. If you are using Canvas, you have to use Height & Width explicitly for the controls.</p> <p>Don’t forget to share this article link to others who might feel it very useful.</p> <p>Appreciate your Feedbacks and/or Suggestions to improve this Article.</p> http://www.silverlightshow.net/items/Step-by-Step-Guide-to-Silverlight-Shape-Controls.aspx editorial@silverlightshow.net (Kunal Chowdhury ) http://www.silverlightshow.net/items/Step-by-Step-Guide-to-Silverlight-Shape-Controls.aspx#comments http://www.silverlightshow.net/items/Step-by-Step-Guide-to-Silverlight-Shape-Controls.aspx Thu, 30 Sep 2010 11:30:00 GMT Building a DataGrid Control for Silverlight for Windows Phone - Part 2 <p><em><strong>This article is compatible with the latest version of Silverlight for Windows Phone 7.</strong></em></p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More on this topic..</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/WP7-What-is-Windows-Phone-7.aspx">What is Windows Phone series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/WP7-Stock-Quoting-Demo-Series-Part1.aspx">WP7 Stock Quoting Demo series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Gergely-Orosz-on-Cocktail-Flow-WP7-application.aspx">Cocktail Flow WP7 application</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/datagrid_control.aspx">The DataGrid series is now available as a fully offline resource: </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/datagrid_control.aspx"><img style="border:0px solid;" alt="Building a DataGrid Control for Silverlight for Windows Phone Ebook" src="http://www.silverlightshow.net/Storage/datagrid_ebook_thumb0.png" usemap="#rade_img_map_1291385581316" /></a><br /> <strong><span style="font-size: 13px;">($0.99)</span></strong></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> <p>This article is Part 2 of the series Building a DataGrid Control for Silverlight for Windows Phone.</p> <h3>Introduction</h3> <p>In my previous <a href="http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx" target="_blank">article</a> I introduced three possible ways to build and use a DataGrid Control on the Windows Phone 7 platform. Then I showed how to create a “Windows Phone 7 ready” DataGrid starting from the source code of the control included in the Silverlight toolkit. I used the last edition available for Silverlight 3 since some features of Silverlight 4 are missing in Windows Phone at the moment. This time I will focus on the porting of the <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Grid/">AgDataGrid free version</a> by <a href="http://www.devexpress.com/">DevExpress</a>. AgDataGrid is an alternative to the standard Silverlight DataGrid Control and offers a series of interesting features. I will show you how to make the control work in the Windows Emulator using the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c8496c2a-54d9-4b11-9491-a1bfaf32f2e3&displaylang=en">Windows Phone Developer Tools Beta July 2010 Edition</a> and starting from the source code of the original AgDataGrid. Unfortunately the license which comes with the control does not allow me to redistribute the source code modified for working on Windows Phone, but if you follow my hints you should be able to get the same result. <br /> <strong><a href="http://www.vimeo.com/14962052">Here</a></strong> you can watch a video showing some of the features of the AgDataGrid control running on the Windows Phone emulator, like grouping by column, different column types, row previews and so on.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/AgDataGridOnEmulator1_2.jpg"><img width="219" height="404" title="AgDataGridOnEmulator1" style="border:0px solid; display: inline;" alt="AgDataGridOnEmulator1" src="http://www.silverlightshow.net/Storage/Users/walterf/AgDataGridOnEmulator1_thumb.jpg" /></a></p> <h3>The AgDataGrid Control</h3> <p>The AgDataGrid Control is a nice tool made available by DevExpress as an alternative of the standard Silverlight Datagrid Control. You can view all the features <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Grid/">here</a> . Beside the commercial version, DevExpress provides a free version with the full source code after registration at the following <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Grid/">page</a>. This version also has a lot of features, the most significant are:</p> <ul> <li>data sorting against multiple columns </li> <li>data grouping against multiple columns </li> <li>column auto-width, movement and resizing </li> <li>row editing </li> <li>row preview </li> <li>multiple column types/editors. </li> </ul> <h3>Porting the AgDataGrid on Windows Phone</h3> <p>How nice would it be to see some of these features inside the Windows Phone 7 platform. Let’s find out. The first thing to do is to get the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c8496c2a-54d9-4b11-9491-a1bfaf32f2e3&displaylang=en">Windows Phone Developer Tools Beta July 2010 Edition</a> and then get the <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Register/">source code</a> of AgDataGrid. I suggest opening the AgDataGrid Solution in Visual Studio 2010 and keep it open as a reference and at the same time creating a new solution in another instance of Visual Studio 2010 using the category“Windows Phone Class Library” listed in the “Silverlight for Windows Phone” templates, as shown below. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/VSLibProject_2.jpg"><img width="644" height="446" title="VSLibProject" style="border:0px solid; display: inline;" alt="VSLibProject" src="http://www.silverlightshow.net/Storage/Users/walterf/VSLibProject_thumb.jpg" /></a></p> <p>Then, you should replicate the folder structure that you see in the “DevExpress.AgDataGrid” project included in the original solution, after putting a copy of all the original files in their respective directory of the new solution. Now, if you try to compile you will probably get a lot of errors and warnings, but do not panic! Things are easier than it seems. <br /> Since the code includes some unit testing and there is no official support for it in Silverlight for Windows Phone at the moment, you need to find a workaround. In the blog of Jeff Wilcox there is the solution: a Unit Test Framework for Silverlight 3 and Silverlight for Windows Phone available <a href="http://www.jeff.wilcox.name/2010/05/sl3-utf-bits/">here</a>. What you just need to do after the download is to add the reference to <em>Microsoft.Silverlight.Testing.dll</em> and <em>Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll</em>. Getting a successful compilation is not enough; you also need to comment out any reference to the <em>AgDataPicker</em> class since it derives from the <em>DatePicker</em> Control which is not supported in Silverlight for Windows Phone. What do you lose with that? You lose the possibility to use a small calendar control when you edit a data type value in a cell; but I think nobody will burst out crying for this. <br /> Another thing to pay attention to is that there is no support for mouse wheel in Silverlight for Windows Phone (I would have been surprised otherwise) and obviously you should comment out any reference to this in the code. <br /> After a small amount of other fittings, you should be able to get a successful build. As you can see in the following image most of the features are still available. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/AgDataGridOnEmulator2_2.jpg"><img width="518" height="484" title="AgDataGridOnEmulator2" style="border:0px solid; display: inline;" alt="AgDataGridOnEmulator2" src="http://www.silverlightshow.net/Storage/Users/walterf/AgDataGridOnEmulator2_thumb.jpg" /></a></p> <p>At a first glance, what seems to work is:</p> <ul> <li>data grouping against multiple grid columns </li> <li>data sorting against multiple grid columns </li> <li>column movement </li> <li>column resizing (but it's difficult) </li> <li>column auto width </li> <li>row editing </li> <li>focused row and focused cell </li> <li>vertical/horizontal lines </li> <li>multiple column types/editors </li> <li>preview display mode </li> </ul> <p>Of course, not everything works perfectly and some adjustments are needed in terms of size and look and feel in order to get compliant with the size of the small screen and the guidelines of the METRO design but it is a good starting point.</p> <h3>Exploring the main features</h3> <p>The original AgDataGrid package is accompanied by some examples with source code which provide a starting point on how to put in practice the main features of the control. Not all are ready to work inside the Windows Phone emulator but with some small adjustments you should be able to make them work. </p> <p>The first is the so called “Column layout”. Here the column widths can be automatically modified to match the owner grid’s width. This is useful when you orient in landscape mode the device and the width of the grid changes consequently.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ColumnLayout_2.jpg"><img width="404" height="218" title="ColumnLayout" style="border:0px solid; display: inline;" alt="ColumnLayout" src="http://www.silverlightshow.net/Storage/Users/walterf/ColumnLayout_thumb.jpg" /></a></p> <p>The second one, “Grouping”, groups data by one or more specific columns. This is also useful on a small screen although the operation of drag and drop over the touch screen may be difficult.</p> <p>The third one, “Column Types” shows you various column types available including custom editors for a given column.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ColumnTypes_2.jpg"><img width="404" height="219" title="ColumnTypes" style="border:0px solid; display: inline;" alt="ColumnTypes" src="http://www.silverlightshow.net/Storage/Users/walterf/ColumnTypes_thumb.jpg" /></a></p> <p>“Preview” and “Preview Layout” are samples on how to show a preview for each data row either with a panel preview. </p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/preview_2.jpg"><img width="212" height="394" title="preview" style="border:0px solid; display: inline;" alt="preview" src="http://www.silverlightshow.net/Storage/Users/walterf/preview_thumb.jpg" /></a></p> <h3>Conclusion</h3> <p>In this article we focused on porting the AgDataGrid Control on the Windows Phone platform. We provided some guidelines to adapt and rebuild the source code of the AgDataGrid to make it work on Windows Phone. Finally we explored some of the features available under the new platform. achieving the first goal. </p> http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-2.aspx editorial@silverlightshow.net (Walter Ferrari ) http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-2.aspx#comments http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-2.aspx Fri, 17 Sep 2010 11:54:00 GMT Building a DataGrid Control for Silverlight for Windows Phone - Part 1 <p><em><strong><em><strong>This article is compatible with the latest version of Silverlight for Windows Phone 7</strong></em>.</strong></em></p> <div style="border:1px solid #dddddd;padding-bottom: 5px; background-color: #f3f3f3; margin-top: 5px; padding-left: 10px; width: 200px; float: right; margin-left: 10px; padding-top: 5px;"> <h3>More on this topic..</h3> <ul style="list-style-type: circle; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; font-size: 12px;"> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/WP7-What-is-Windows-Phone-7.aspx">What is Windows Phone series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/WP7-Stock-Quoting-Demo-Series-Part1.aspx">WP7 Stock Quoting Demo series</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/items/Gergely-Orosz-on-Cocktail-Flow-WP7-application.aspx">Cocktail Flow WP7 application</a> </li> <li style="padding-bottom: 5px;"><a href="http://www.silverlightshow.net/ebooks/datagrid_control.aspx">The DataGrid series is now available as a fully offline resource: </a></li> </ul> <p style="padding-bottom: 5px; text-align: center;"><a href="http://www.silverlightshow.net/ebooks/datagrid_control.aspx"><img style="border:0px solid;" alt="Building a DataGrid Control for Silverlight for Windows Phone Ebook" src="http://www.silverlightshow.net/Storage/datagrid_ebook_thumb0.png" usemap="#rade_img_map_1291385581316" /></a><br /> <strong><span style="font-size: 13px;">($0.99)</span></strong></p> <p style="font-size: 12px;">     <a href="http://www.silverlightshow.net/ebooks.aspx">All SilverlightShow Ebooks</a> <img alt="" src="http://www.silverlightshow.net/Storage/arrow-content.jpg" /></p> </div> This article is Part 1 of the series Building a DataGrid Control for Silverlight for Windows Phone. <h3>Introduction</h3> <p>As you probably know there is no DataGrid available under the Silverlight for Windows Phone platform at the moment. One could object that a DataGrid in Windows Phone is not so much requested for a lot of reasons, size of the screen, user input mode etc. This is true but it is also true that new projects regarding local databases appear day by day on the horizon (i.e. <a href="http://www.mcobject.com/june30/2010">Perst for WP7</a> , <a href="http://winphone7db.codeplex.com/">Windows Phone 7 Database</a>) and this opens the space for the development of a class of business applications, even for small devices. With this scenario a specialized UI control to visualize data becomes significant. The challenge here is to build a control which is at the same time clear, understandable and easy-to-use. This is the first of a series of articles in which we will try to achieve the desired result by exploring various ways. In this article I will introduce three possible ways. Then I will focus on a solution in particular, i.e. the porting on the Windows Phone 7 platform of the DataGrid included in the Silverlight Toolkit. The source code for this article can be downloaded from <a href="http://www.silverlightshow.net/Storage/Sources/DataGrid_Silverlight_ported_to_PHONE.zip" target="_blank">here</a>.</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/datagrid_2.jpg"><img width="257" height="480" title="datagrid" style="border:0px solid; display: inline;" alt="datagrid" src="http://www.silverlightshow.net/Storage/Users/walterf/datagrid_thumb.jpg" /></a></p> <h3>Three possible solutions</h3> <p>In brief, from where to start? A radical solution could be starting from scratch but this would require a considerable effort. Furthermore, when considering a new Windows Phone application you have to take care of what is written in the “UI design and Interaction Guide for Windows Phone Series”; it clearly says that diverging from the Windows Phone 7 Series CTP interaction model is generally not allowed. An excellent starting point could be the <a href="http://www.codeproject.com/KB/silverlight/SLDataGridStep1.aspx">tutorial</a> made by Jeff Karlson which explains in a very detailed and complete way how to build your own DataGrid for Silverlight.</p> <p>From the foregoing, it is preferable, in a more pragmatic way, to take a look around and look for something similar in the neighbourhood. For instance, starting from the DataGrid included in the WPF Toolkit could be a possibility; in fact, it is provided with the source code. Another way is focusing on the DataGrid included in the Silverlight Toolkit and looking for the related source code in the subdirectories. Another solution is to take into consideration products from third parties like <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Grid/" target="_blank">AgDataGrid</a> of DevExpress. What we really will do in the article is highlight the difficulties behind the conversion of the WPF Data grid, then give a taste of the porting of the Silverlight Toolkit DataGrid. In a next article we will face the porting of AgDataGrid to Windows Phone.</p> <h3>Features that a DataGrid Control must have</h3> <p>What is the main feature a DataGrid should have? Probably the “autogeneration of columns” is a strong candidate. In fact, knowing that the control is self-sufficient is in most cases very useful, but it is not the only thing. A header columns and the possibility to handle various column types are other inalienable characteristics. As for the appearance, styling properties should be present. Eventually a due degree of interactivity must be granted: the user should be able to select rows, change the content of a cell, show a row detail.</p> <h3>First solution : a look to the WPF DataGrid</h3> <p>Why should a Windows Phone developer be interested in WPF Datagrid? Well, the main reasons are that Silverlight, as commonly known, is often presented as a subset of WPF and that the source code of WPF Datagrid is available; this looks good on paper. Let’s give a look then at the WPF Datagrid. Looking at the hierarchical tree of the main class, DataGrid visibly derives from two abstract classes, <em>MultiSelector</em> and <em>Selector</em>, which extend the base class <em>ItemsControl</em> by providing selection capabilities.</p> <p>The first thing to pay attention to is that the code makes massive use of property system capabilities which do not exist in Silverlight and for which you have to find a workaround if you want to make the conversion:</p> <ul> <li>several default dependency properties coming from the base class <em>ItemsControl</em> are modified through the <em>OverrideMetadata</em> method. Unfortunately the Silverlight for Windows Phone (not even Silverlight 4) version of <em>DependencyProperty</em> class does not support this method. A workaround could be, as suggested <a href="http://devblog.ailon.org/devblog/post/2009/11/30/Writing-WPFSilverlight-compatible-code-Part-5-Adding-PropertyChangedCallback.aspx">here</a>, to involve an extra DependencyProperty declaration and binding of the original dependency property to this one. </li> <li>there are a lot of Coerce-value callbacks. You have to implement an alternative in this area by using property-changed callbacks, which are supported in Silverlight. </li> <li>most custom dependency properties are read-only. You cannot do the same in Silverlight, any external code can set the dependency property through <em>SetValue</em> method. </li> <li><em>FrameworkPropertyMetadata</em> is often used for the type used for dependency property metadata, rather than the base metadata types PropertyMetadata. A workaround can be found <a href="http://devblog.ailon.org/devblog/post/2009/11/05/Writing-WPFSilverlight-compatible-code-Part-2-Dependecy-Properties.aspx">here</a>. </li> </ul> <p>Additionally, editing actions and other actions are handled through commands using the <em>CommandManager</em> class which is missing in Silverlight. Here you also have to find a solution. In this <a href="http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx">blog</a> there are some interesting hints you may attempt to use. </p> <p>Before going any further to describe other platform-specific features used by the code, I think the above-described functioning is enough to say that the porting of WPF DataGrid to the Silverlight for Windows Phone 7 is not that funny as it appeared at the beginning and maybe we should try other ways.</p> <h3>Second solution: a look to the Silverlight DataGrid</h3> <p>The good news is that the way is much simpler than the one described in the previous paragraph. The bad news is that the source code for the current Toolkit’s DataGrid is targeted for Silverlight 4 whilst Windows Phone 7 runs a customized version of Silverlight 3 with just some capabilities of Silverlight 4. But do not worry, you can retrieve an older release from CodePlex (i.e. the one for Silverlight 3) and get the source code. If you do so and then look at the code you will notice that surprisingly (or not…) the scheme is quite different from the one used for WPF DataGrid. In fact, the Silverlight DataGrid version derives directly from <em>Control </em>class where the WPF DataGrid, as we have seen, derives from the ItemsControl class through two abstract classes, <em>MultiSelector</em> and <em>Selector</em>. There are a lot of other differences, of course but I do not want to annoy you with useless details; I just want to give you some guidance on how to get working the source code on Windows Phone 7. First of all you have to create a new Windows Phone Class Library Project, then locate the “Source code.zip” file included in the directory of your Silverlight Toolkit installation and expand it. Copy all the files and subdirectories under <em>< toolkit source code files >\ System.Windows.Controls.Data</em> into the directory of your project and import them in Visual Studio. Add the following references:</p> <ul> <li><…>\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.Windows.Controls.Data.dll </li> <li><…>\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.ComponentModel.DataAnnotations.dll </li> <li><…>\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.Windows.Controls.Data.Input.dll </li> </ul> <p>Change the namespace in each file from “<em>System.Windows.Controls”</em> to something else (I used <em>PhoneDataGridControl)</em> in order to avoid some strange cross references with the <em>System.Windows.Controls.Data.dll</em> we added above; now if you try to build the solution you surely get a lot of errors and warnings; so you must have the patience to add basic references here and there, Visual Studio will help you. If you want to skip these steps you can download the working code from the link at the beginning of the article. Here below you can see the DataGrid running on the emulator with paging enabled .</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/datagridWithPaging_2.jpg"><img width="257" height="480" title="datagridWithPaging" style="border:0px solid; display: inline;" alt="datagridWithPaging" src="http://www.silverlightshow.net/Storage/Users/walterf/datagridWithPaging_thumb.jpg" /></a></p> <h3>Third solution: a look to the AgDataGrid DataGrid</h3> <p>The AgDataGrid Control is a beautiful tool made available by DevExpress as a substitute of the standard Silverlight Datagrid Control. You can view all the features <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Grid/">here</a> . Next to the commercial version, DevExpress provides a free version with the full source code after registration at the following <a href="http://www.devexpress.com/Products/NET/Controls/Silverlight/Register/">page</a>. I do not want to say much here because the topic will be discussed in depth in the next article. I just want you to know that with some tricks the porting is possible.</p> <h3>Conclusion</h3> <p>In this article we have considered the possibility to create a DataGrid Control for the Windows Phone 7 platform. We have spotted three possible ways, i.e. starting from the WPF DataGrid or the Silverlight Control or finally the AgDataGrid Control. We have investigated each of these ways and we have soon discarded the WPF solution; we focused on porting the Silverlight DataGrid Control reaching the first goal; we let the reader imagine that the third solution is feasible but we will check this in the next article.</p> http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx editorial@silverlightshow.net (Walter Ferrari ) http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx#comments http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx Mon, 16 Aug 2010 05:29:00 GMT Understanding control customization with templates: part #2 <p style="text-align: justify;"><em><span style="line-height: 115%; font-family: calibri, sans-serif; font-size: 11pt;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></p> <p style="text-align: justify;">In the <a href="http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx">previous part of this article</a>, I've explained the role of templates in Silverlight controls showing how to radically change the appearance of a control without having to change or rewrite its logic. This is an important feature of Silverlight's controls that allow a developer to create an application without concerning about its appearance and leave the designer free of manipulate it without needing to know their intimate logic. Unfortunately there are many times when built-in controls, the SDK and the Toolkit do not contains the control we need for our application. So you have to start writing the control by yourself, and creating a templated control make your work more reusable and customizable for future usages.</p> <p style="text-align: justify;"><a href="http://www.silverlightshow.net/Storage/Sources/SilverlightPlayground.ControlTemplates.zip">Download Source Code</a></p> <h4 style="text-align: justify;">Starting a new control</h4> <p style="text-align: justify;"><img width="337" height="104" title="Capture" style="margin: 0px; display: inline; float: right;" alt="Capture" src="http://www.silverlightshow.net/Storage/Users/AndreaBoschin/_Capture_1.png" /> The set of controls of Silverlight is very wide and cover a lot of needs, so it is hard to find a missing control to show in this article. The inspiration comes from the new generation phones, which often include controls that are very innovative. I decided to try a port of the Special button switch that you have certainly appreciated by accessing an iPhone.</p> <p style="text-align: justify;">The figure shows two instances of a control, I will call Switch, one in the Checked state (the red one) and the other in the Unchecked state. You can change the state of the control acting two ways: clicking on the coloured area or dragging the thumb with the chevron in the direction you want. The beautiful of the control is that it shows an animation when you make a change in its state scrolling the thumb from a side to the other. </p> <p style="text-align: justify;">The control, as you can figure out, is a sort of ToggleButton, so we should start its building from this class. I choosed of starting from scratch implementing a ContentControl directly for two reasons. First of all I would want to make the example simple and basic, and secondly I noticed there isn't an easy way to implement the behavior I described using VisualStateManager. So let me start writing code:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Switch : ContentControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> Switch()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(Switch);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">In this snippet you see two basic concepts. My class inherits from ContentControl because I need to have some content, like a normal ToggleButton. This content is the label on the side or a more complex structure of objects. When you are starting a new control this is the first choice you have to make: Control (or something inheriting from Control) if the control is a leaf in the VisualTree. ContentControl if it can have a content. </p> <p style="text-align: justify;">On the other side you can see I given a value to the DefaultStyleKey property. This lets the runtime will search its template in a file we are about to add to the project. The file, called always  generic.xaml, must be in a folder called "Themes" in the root of the project that contains the control. Its purpose is containing the basic templates of all the controls in the assembly. So, Here is the initial generic.xaml</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ResourceDictionary</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns:local</span><span style="color: #0000ff;">="clr-namespace:SilverlightPlayground.ControlTemplates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Switch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Switch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="Black"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #ff0000;">CornerRadius</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Stretch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Stretch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OnColumnElement"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="0*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OffColumnElement"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="100*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OnBackgroundElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Red"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OffBackgroundElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Green"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Thumb</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ThumbElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="40"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Right"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,0,5,0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">ResourceDictionary</span><span style="color: #0000ff;">></span></pre> </div> </div> </div> <p style="text-align: justify;">This template creates the basic appearance of the control, as you have seen in the previous figure. It misses some important parts, but I will add something else in the following snippets. In the template I have a basic Grid that separates the label from the body of the control. The body is made of another grid with three columns. The size of the first and the last column will be changed to move the thumb from the left to the right or viceversa. In the template I also have a Thumb control, representing an element that can be dragged and a couple of rectangles that give the background color to the visible area. </p> <p style="text-align: justify;">Now we need to take some references to the elements we need to use for the animation. They are the Columns, the thumb and the coloured rectangles:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span>[TemplatePart(Name = Switch.ThumbElementName, Type = <span style="color: #0000ff;">typeof</span>(Thumb))]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>[TemplatePart(Name = Switch.OnColumnElementName, Type = <span style="color: #0000ff;">typeof</span>(ColumnDefinition))]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span>[TemplatePart(Name = Switch.OffColumnElementName, Type = <span style="color: #0000ff;">typeof</span>(ColumnDefinition))]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span>[TemplatePart(Name = Switch.OnBackgroundElementName, Type = <span style="color: #0000ff;">typeof</span>(Shape))]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span>[TemplatePart(Name = Switch.OffBackgroundElementName, Type = <span style="color: #0000ff;">typeof</span>(Shape))]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> Switch : ContentControl</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> ThumbElementName = <span style="color: #006080;">"ThumbElement"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> Thumb ThumbElement { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> OnColumnElementName = <span style="color: #006080;">"OnColumnElement"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> ColumnDefinition OnColumnElement { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> OffColumnElementName = <span style="color: #006080;">"OffColumnElement"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> ColumnDefinition OffColumnElement { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> OnBackgroundElementName = <span style="color: #006080;">"OnBackgroundElement"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> Shape OnBackgroundElement { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> OffBackgroundElementName = <span style="color: #006080;">"OffBackgroundElement"</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> Shape OffBackgroundElement { get; set; }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> Switch()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(Switch);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnApplyTemplate()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.ThumbElement = <span style="color: #0000ff;">this</span>.GetTemplateChild(Switch.ThumbElementName) <span style="color: #0000ff;">as</span> Thumb;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.ThrowIfMissing(<span style="color: #0000ff;">this</span>.ThumbElement, Switch.ThumbElementName);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OffColumnElement = <span style="color: #0000ff;">this</span>.GetTemplateChild(Switch.OffColumnElementName) <span style="color: #0000ff;">as</span> ColumnDefinition;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.ThrowIfMissing(<span style="color: #0000ff;">this</span>.OffColumnElement, Switch.OffColumnElementName);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OnColumnElement = <span style="color: #0000ff;">this</span>.GetTemplateChild(Switch.OnColumnElementName) <span style="color: #0000ff;">as</span> ColumnDefinition;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.ThrowIfMissing(<span style="color: #0000ff;">this</span>.OnColumnElement, Switch.OnColumnElementName);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OnBackgroundElement = <span style="color: #0000ff;">this</span>.GetTemplateChild(Switch.OnBackgroundElementName) <span style="color: #0000ff;">as</span> Shape;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OffBackgroundElement = <span style="color: #0000ff;">this</span>.GetTemplateChild(Switch.OffBackgroundElementName) <span style="color: #0000ff;">as</span> Shape;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum39" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.UpdateVisualState();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum40" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum41" style="color: #606060;"> </span> <span style="color: #0000ff;">base</span>.OnApplyTemplate();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum42" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum43" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum44" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> UpdateVisualState()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum45" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum46" style="color: #606060;"> </span> <span style="color: #008000;">// update the control appearance here</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum47" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum48" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum49" style="color: #606060;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> ThrowIfMissing(DependencyObject element, <span style="color: #0000ff;">string</span> name)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum50" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum51" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (element == <span style="color: #0000ff;">null</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum52" style="color: #606060;"> </span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Missing required element '{0}'"</span>, name));</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum53" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum54" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">The OnApplyTemplate method is useful for this purpose. It is called in the initial phase of the life of the control and lets the developer to connect some elements of the control I have called "parts" in the previous article. An element is a "part" when it is attached in the OnApplyTemplate method and it is declared with the TemplatePart attribute at the top of the class. This attribute is not used by the runtime but is useful to external tools to detect the parts for design purpose. </p> <p style="text-align: justify;">Many times a part is optional and can be easily removed without causing an error in the control. Some other times it is required. The control has to handle both the cases. When a part is optional the logic have to gracefully skip it when its value is null. When a part is required the better thing to do is raising an exception during the OnApplyTemplate method, as I did in the ThrowIfMissing method. Remember that the more the parts are optional more your control will be customizable. And also when you choose a class to take a reference to a part choose always the closest to the root of the inheritance hierarchy that has all the methods and property you need to make it working.</p> <p style="text-align: justify;">Finally, UpdateVisualState is the method where the visual appearance of the control is updated every time a property changes its value. The method is useful every time there is something changed and it needs to be reflected into the UI. In my control, this method will starts an animation to move the thumb. This update is needed only when the IsChecked property (a dependency property) is changed and when the control is initialized to make it respectful of the current value of the same property. </p> <p style="text-align: justify;">In Silverlight there is not any way to animate a GridLenght property and this is the reason we need to create a custom animation to make the sliding. I create an empty storyboard and I use it as a timer to change the with of the colums, increasing one and decreasing the other. When the thumb has reached its position the animation is stopped:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> UpdateVisualState()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.SwitchStoryBoard.Begin();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span>}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> SwitchStoryBoard_Completed(<span style="color: #0000ff;">object</span> sender, EventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">this</span>.IsChecked)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.OffColumnElement.Width.Value == 100.0)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.SwitchStoryBoard.Stop();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OffColumnElement.Width = <span style="color: #0000ff;">new</span> GridLength(<span style="color: #0000ff;">this</span>.OffColumnElement.Width.Value + 10, GridUnitType.Star);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OnColumnElement.Width = <span style="color: #0000ff;">new</span> GridLength(<span style="color: #0000ff;">this</span>.OnColumnElement.Width.Value - 10, GridUnitType.Star);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.SwitchStoryBoard.Begin();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.OnColumnElement.Width.Value == 100.0)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.SwitchStoryBoard.Stop();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OnColumnElement.Width = <span style="color: #0000ff;">new</span> GridLength(<span style="color: #0000ff;">this</span>.OnColumnElement.Width.Value + 10, GridUnitType.Star);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.OffColumnElement.Width = <span style="color: #0000ff;">new</span> GridLength(<span style="color: #0000ff;">this</span>.OffColumnElement.Width.Value - 10, GridUnitType.Star);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.SwitchStoryBoard.Begin();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">Another thing to do, is to bind some properties of the template to the properties of the control. I used the TemplateBinding for the BorderBrush and BorderThickness, then I created two dependency properties in the control, OnBackground e OffBackground, to leave the user free of customizing the colors of the control. I used again the TemplateBinding, same way I did with the other properties. </p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Switch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="BorderThickness"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="OnBackground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="#FFFF0000"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="OffBackground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="#FF008800"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="BorderBrush"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearGradientBrush</span> <span style="color: #ff0000;">EndPoint</span><span style="color: #0000ff;">="0.5,1"</span> <span style="color: #ff0000;">StartPoint</span><span style="color: #0000ff;">="0.5,0"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFA3AEB9"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF8399A9"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF718597"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearGradientBrush</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Template"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="local:Switch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="<strong>{TemplateBinding BorderThickness}</strong>"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="<strong>{TemplateBinding BorderBrush}</strong>"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> <span style="color: #ff0000;">CornerRadius</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Stretch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Stretch"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OnColumnElement"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="0*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="Auto"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OffColumnElement"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="100*"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OnBackgroundElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="<strong>{TemplateBinding OnBackground}</strong>"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OffBackgroundElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="<strong>{TemplateBinding OffBackground}</strong>"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Thumb</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ThumbElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="40"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OffChevron"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M-5,-5 L6,3 L-5,11 L3,3 z"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> </span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="14,4"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">UseLayoutRounding</span><span style="color: #0000ff;">="False"</span><span> </span><span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="False"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum39" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="OnChevron"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M6.114583,-5 L-2.25,3 L5.8854165,11 L-5,3 z"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum40" style="color: #606060;"> </span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF617584"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="14,4"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">UseLayoutRounding</span><span style="color: #0000ff;">="False"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0.0"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="False"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum41" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="DisabledVisualElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="3"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum42" style="color: #606060;"> </span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0.0"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#eeFFFFFF"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="False"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum43" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ContentFocusVisualElement"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="3"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="2"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum44" style="color: #606060;"> </span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#FF6DBDD1"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="false"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum45" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum46" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum47" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Right"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,0,5,0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum48" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum49" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum50" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum51" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum52" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> </div> </div> </div> <p style="text-align: justify;">Using the Style this way is very comfortable because it lets the developer to define some default values for properties. In the Setter on top of the template I defined some colors, and attributes that the user can change, but they have defaults to prevent unexpected results.</p> <h4 style="text-align: justify;">The Visual States</h4> <p style="text-align: justify;">Now that the control works as expected, it is time to make it more customizable. This capability comes from the use of the VisualStateManager that is able to manage some state groups. First of all I added the couple Normal/Disabled but also the Focused and Unfocused status. While the first couple is implemented using a rectangle on top of all the other parts, the Focused status uses another rectangle to create a border around the control. </p> <p style="text-align: justify;">Another couple of styles, I've implemented, are Checked/Unchecked. Also if these conditions are showed by the sliding thumb the developer can find useful to have these statuses for the purpose of add some other effects. In my case I added a chevron that switches from left to right. Here is the code for the VisualStateManager:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateManager.VisualStateGroups</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CommonStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Normal"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Disabled"</span> <span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="0.5"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="DisabledVisualElement"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="FocusStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Unfocused"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Focused"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="ContentFocusVisualElement"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Checked"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="OffChevron"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="OnChevron"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Unchecked"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="OffChevron"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="OnChevron"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateManager.VisualStateGroups</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">This chunk of code must be inserted in the root element of the ControlTemplate. As you can see, it defines some animations the runtime will show during the transition. The VSM is capable of interpolate all the possible transitions and due to this reason it is possible to not define all the transitions between states. </p> <p style="text-align: justify;">Once you have defined the states in the markup into the generic.xaml, you have two final things to do. You have to notify the VisualStateManager about the transitions from a state to another and you have to publish the available states using the TemplateState attribute as you already did with parts and the TemplatePart attribute. Starting by the last, the TemplateState attribute enables the graphics tools to know how many statuses they have to handle. </p> <p style="text-align: justify;">But what really matter is setting the state of the control because it enables the VisualStateManager to apply the defined animations and transitions. In an event handler you can simply call the GoToState static method on the VisualStateManager class. Here is and example:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Switch_IsEnabledChanged(<span style="color: #0000ff;">object</span> sender, DependencyPropertyChangedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.ThumbElement.IsEnabled = (<span style="color: #0000ff;">bool</span>)e.NewValue;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.ThumbElement.IsEnabled != <span style="color: #0000ff;">true</span>)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"Disabled"</span>, <span style="color: #0000ff;">true</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"Normal"</span>, <span style="color: #0000ff;">true</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span>}</pre> </div> </div> </div> <p style="text-align: justify;">This code sets the correct state according to the IsEnabled property value. The three arguments of the GoToState method are: </p> <ul> <li> <div style="text-align: justify;">The UIElement which you have to change the state</div> </li> <li> <div style="text-align: justify;">The destination state as you have called it in the markup</div> </li> <li> <div style="text-align: justify;">A boolean indicating if the transitions must be used or if the change must be immediate</div> </li> </ul> <h4 style="text-align: justify;">Template or not template?</h4> <p style="text-align: justify;">The process of creating a templated control may appear hard, but once you have understood how it works you will find there is not any reason to create controls without a template if they have an UI. Having the basic template expressed as xaml markup makes the development process more simple and straightforward then having to create parts in code. The trick, about control reusability, is to think at them always like behaviors and then add a default appearance, but remember not to close any way to future drastic changes.</p> http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-2.aspx editorial@silverlightshow.net (Andrea Boschin ) http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-2.aspx#comments http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-2.aspx Thu, 15 Jul 2010 11:36:00 GMT Understanding control customization with templates: part #1 <p style="text-align: justify;"><em><span style="line-height: 115%; font-family: calibri, sans-serif; font-size: 11pt;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></p> <p style="text-align: justify;"><em><span style="line-height: 115%; font-family: calibri, sans-serif; font-size: 11pt;"></span></em>Every Silverlight developer - and more extensively each Windows developer - knows what I mean with the term "Control". The term is referred to a reusable component that let the developer to add a known behavior to the user interface. The Silverlight plugin, the SDK and the Control's Toolkit have plenty of these controls, and we are used to put them into our application without any fear. Unfortunately, often their appearance is ugly and we may be tempted to change it someway, to give our application a fresh and unique look & feel. With Silverlight there are various degree of personalization you can apply to them, from the simple markup attribute, to an implicit style, but often we need a more deep and drastic revision. In these cases Silverlight grant a unique capability of changing the very inner structure of every control to bend it to our will.</p> <h4 style="text-align: justify;">Behavior versus Appearance</h4> <p style="text-align: justify;"><img width="198" height="211" title="Capture" style="margin: 0px 15px 0px 0px; display: inline; float: left;" alt="Capture" src="http://www.silverlightshow.net/Storage/Users/AndreaBoschin/Capture_1.png" /> </p> <p style="text-align: justify;">Given that the middle control in figure is a ToggleButton, what they have in common the three controls? At a superficial exam it may appear that the sole thing they have in common is the text "Select this option...". From an user experience point of view, the meaning they have in an user interface is someway different. But if you analyze them, from a developer perspective, you may agree they are perfectly equals. All the controls infact can be switched on or off and everyone has a label. So, the very important difference is not about the behavior but is about the aspect they have. And If you investigate deeply using Visual Studio you will understand that CheckBox and RadioButton are both inherited from ToggleButton, that is the root of this kind of controls. </p> <p style="text-align: justify;">In Silverlight, there is a clear distinction between the behavior and the aspect of every control, at least if the control is developed with a set of rules in mind, that make it a Templated Control. This distinction separates the work of two key figures in the development process: the developer and the designer. Both these figures have different roles in the team and often the requirements of the first are totally opposite to the other's. </p> <p style="text-align: justify;">I'm a developer, and when I put a CheckBox in the interface I'm acquiring a behavior to my application. I simply need that someone can choose if the user needs to activate or not an option. Nothing else. At the opposite, when a designer looks at the same interface it does not matter what the control does, but his main concern is its appearance, to make the application not only beautiful, but also more easily understandable and usable.</p> <h4 style="text-align: justify;">Templated controls.</h4> <p style="text-align: justify;">In one of the last sentences, I used the term "templated control". First of all you have to know that all the controls in the Silverlight core, in the SDK and in the Control's Toolkit are fundamentally Templated Controls. But what does this mean? </p> <p style="text-align: justify;">In every control you can detect some fixed parts, and some other parts that can be changed by the developer. In the CheckBox you have a Content property that let you change the label, but you can customize colors, borders, and many other properties. If you think at the inner structure of a the checkbox, made of simple shapes held toghether by some layout elements, you understand that changing a property of the control means changing one or more property of one or more shapes in this structure. This "structure" is called "ControlTemplate", because of the fact you can think at it as a placeholder for properties you can change and parts you can add. The template appearance will remain the same across different instances of the control but the label can change its content, the border its thickness and so on. Obviously you can create your own Templated Controls, but this is matter for the next part of the article. In this paragraphs you will learn how to customize the template and deeply change the appearance of the existing controls.</p> <p style="text-align: justify;">Until now I said that every control has a template, but I never tried to explain where this template is located. So, if you watch to the properties of a CheckBox you will easily find a Template property, and this is the location where an instance of ControlTemplate class retain the appearance of the control. Now, please point your browser to Google and type "silverlight control styles and templates" and hit "I'm feeling lucky". The first link of the search probably will take you to this page: <a href="http://msdn.microsoft.com/en-us/library/cc278075(VS.95).aspx">http://msdn.microsoft.com/en-us/library/cc278075(VS.95).aspx</a>. Here you can find a complete list of the Controls in Silverlight and a link to a page containig the original template of every control. Hit CheckBox Style and Templates and you will be revealed the whole template behind the CheckBox. Probably something like this:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="CheckBox"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="16"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Left"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Top"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Background"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="{TemplateBinding BorderThickness}"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="BackgroundOverlay"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FFC4DBEE"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#00000000"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="BoxMiddleBackground"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="{TemplateBinding Background}"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#00000000"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="BoxMiddle"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle.Stroke</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearGradientBrush</span> <span style="color: #ff0000;">EndPoint</span><span style="color: #0000ff;">=".5,1"</span> <span style="color: #ff0000;">StartPoint</span><span style="color: #0000ff;">=".5,0"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearGradientBrush</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Rectangle.Stroke</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle.Fill</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearGradientBrush</span> <span style="color: #ff0000;">StartPoint</span><span style="color: #0000ff;">="0.62,0.15"</span> <span style="color: #ff0000;">EndPoint</span><span style="color: #0000ff;">="0.64,0.88"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#FFFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.013"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#F9FFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.375"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#EAFFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="0.603"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">GradientStop</span> <span style="color: #ff0000;">Color</span><span style="color: #0000ff;">="#D8FFFFFF"</span> <span style="color: #ff0000;">Offset</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearGradientBrush</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Rectangle.Fill</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Rectangle</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="BoxMiddleLine"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">=".2"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckIcon"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1,1,0,1.5"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF333333"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10.5"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M102.03442,598.79645 L105.22962,597.78918 L106.78825,600.42358 C106.78825,600.42358 108.51028,595.74304 110.21724,593.60419 C112.00967,591.35822 114.89314,591.42316 114.89314,591.42316 C114.89314,591.42316 112.67844,593.42645 111.93174,594.44464 C110.7449,596.06293 107.15683,604.13837 107.15683,604.13837 z"</span> <span style="color: #ff0000;">FlowDirection</span><span style="color: #0000ff;">="LeftToRight"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="IndeterminateIcon"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FF333333"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="6"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="DisabledVisualElement"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FFFFFFFF"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ContentFocusVisualElement"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#FF6DBDD1"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="false"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="16"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="16"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Border</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="ValidationErrorElement"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">BorderThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">CornerRadius</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">BorderBrush</span><span style="color: #0000ff;">="#FFDB000C"</span> <span style="color: #ff0000;">Visibility</span><span style="color: #0000ff;">="Collapsed"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> </span> <span style="color: #ff0000;">ToolTipService</span>.<span style="color: #ff0000;">PlacementTarget</span><span style="color: #0000ff;">="{Binding RelativeSource={RelativeSource TemplatedParent}}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ToolTipService.ToolTip</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum38" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ToolTip</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="validationTooltip"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum39" style="color: #606060;"> </span> <span style="color: #ff0000;">Template</span><span style="color: #0000ff;">="{StaticResource ValidationToolTipTemplate}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum40" style="color: #606060;"> </span> <span style="color: #ff0000;">DataContext</span><span style="color: #0000ff;">="{Binding RelativeSource={RelativeSource TemplatedParent}}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum41" style="color: #606060;"> </span> <span style="color: #ff0000;">Placement</span><span style="color: #0000ff;">="Right"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum42" style="color: #606060;"> </span> <span style="color: #ff0000;">PlacementTarget</span><span style="color: #0000ff;">="{Binding RelativeSource={RelativeSource TemplatedParent}}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum43" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ToolTip.Triggers</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum44" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">EventTrigger</span> <span style="color: #ff0000;">RoutedEvent</span><span style="color: #0000ff;">="Canvas.Loaded"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum45" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">EventTrigger.Actions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum46" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">BeginStoryboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum47" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum48" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ObjectAnimationUsingKeyFrames</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="validationTooltip"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="IsHitTestVisible"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum49" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DiscreteObjectKeyFrame</span> <span style="color: #ff0000;">KeyTime</span><span style="color: #0000ff;">="0"</span> <span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum50" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DiscreteObjectKeyFrame.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum51" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">sys:Boolean</span><span style="color: #0000ff;">></span>true<span style="color: #0000ff;"></</span><span style="color: #800000;">sys:Boolean</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum52" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DiscreteObjectKeyFrame.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum53" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">DiscreteObjectKeyFrame</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum54" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ObjectAnimationUsingKeyFrames</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum55" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum56" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">BeginStoryboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum57" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">EventTrigger.Actions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum58" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">EventTrigger</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum59" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ToolTip.Triggers</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum60" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ToolTip</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum61" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ToolTipService.ToolTip</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum62" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="10"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Right"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,-4,-4,0"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Top"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="Transparent"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum63" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,3,0,0"</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M 1,0 L5,0 A 2,2 90 0 1 7,2 L7,6 z"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#FFDC000C"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum64" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Path</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,3,0,0"</span> <span style="color: #ff0000;">Data</span><span style="color: #0000ff;">="M 0,0 L2,0 L 7,5 L7,7"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="#ffffff"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum65" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum66" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Border</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum67" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum68" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum69" style="color: #606060;"> </span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum70" style="color: #606060;"> </span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="contentPresenter"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum71" style="color: #606060;"> </span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="{TemplateBinding Content}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum72" style="color: #606060;"> </span> <span style="color: #ff0000;">ContentTemplate</span><span style="color: #0000ff;">="{TemplateBinding ContentTemplate}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum73" style="color: #606060;"> </span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="{TemplateBinding HorizontalContentAlignment}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum74" style="color: #606060;"> </span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="{TemplateBinding VerticalContentAlignment}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum75" style="color: #606060;"> </span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="{TemplateBinding Padding}"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum76" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum77" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">In the snippet, I've removed some parts of the template, related to the Visual State Manager, to shorten the code and concentrate our attention on the appearance of the control. If you are experienced in XAML you will understand the anatomy of the control, but the better is oversimplify it and change the control with our own template: Here is a shorter XAML snippet:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="#FF666666"</span> <span style="color: #ff0000;">StrokeThickness</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="WhiteSmoke"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckIcon"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Orange"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1,1,0,1"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="contentPresenter"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="3"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">In the first element I declare a ControlTemplate. This element can be inserted directly into the Template property of the control or better added to a Style that we can apply to multiple controls. The inner content is the root of the template of the control and it is easy to understand the new checkbox will appear as a rectangle with an orange section used as selection indicator. The important part is the ContentPresenter control. This kind of control is useful to indicate where the value of the Content property has to be put. It is a real placeholder that will be filled with the sentence "Select this option..." or with something else you can write in the markup. </p> <p style="text-align: justify;">A template may have some "parts", that are particular elements, referenced by the underlying class. The checkbox does not declare any parts, but often there is the need of attaching events to an element so, the developer give it a name, and the element becomes a "part". Watch the page "Slider Styles and Templates" for a real example of some parts. The slider has many parts, and as you can understand they are required to get the user interaction and change the values. </p> <p style="text-align: justify;">What you have to be concerned about the parts is that you have never to change its name and its underlying type. The pages of MSDN accurately describe the names and the types of the parts, so if you find a part is declared "Button" you can use only a Button, but if the same part is declared as ButtonBase you can use indifferently a Button or an HyperlinkButton because both derive from ButtonBase.</p> <p style="text-align: justify;">Once you defined the new appearance of the control there is another need you can have. In my example you may want to change the color and thickness of the border or some other properties without creating different templates for each combination. You probably expect to put the new color in the BorderBrush property of the CheckBox and have the border changed. To allow this you have to create a binding between the property of the control and one or more properties in the template. For this purpose you have to use the TemplateBinding markup extension:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="<strong>{TemplateBinding BorderBrush}</strong>"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="<strong>{TemplateBinding Background}</strong>"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="2"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckIcon"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Orange"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1,1,0,1"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="1.0"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="contentPresenter"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="3"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> </div> <p style="text-align: justify;">The TemplateBinding creates a sort of link between two properties. You only have to be sure both the properties have the same type. Also if this may appear similar to the Binding markup extension, the TemplateBinding is very limited in Silverlight. It has not any Converter property and cannot use anything else the a property name, but it is equally useful to spare time when reusing a template you created.</p> <h4 style="text-align: justify;">Managing states</h4> <p style="text-align: justify;">If you run the project after assigning the template I wrote in the previous box you will remain deluded. The new CheckBox has a new appearance but it does not work anymore. It is become a shape on the plugin surface and is unable to react to the user interaction. The problem is that we have not defined what in the control have to change to react at the various states it might assume. With "state" I mean Checked or Unchecked, Hover or Normal, Focused or disabled. Each state changes some properties of the template and gives a feedback to the user. In the page "CheckBox Styles and Templates" you will find a table containing all the states of the CheckBox. They are managed by a component called VisualStateManger that is responsible of maintaining a number of state groups and track the current. You can put an entire section describing how a state must appear and every state is represented by one or more animations. The great thing is that you only have to be concerned about how an animation must change the appearance, and the VSM will interpolate all the transitions to the states. So, let me add a state to my checkbox template:</p> <div style="text-align: justify;"> <div id="codeSnippetWrapper" style="text-align: left; padding-bottom: 4px; line-height: 12pt; overflow-x: auto; overflow-y: auto; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; cursor: text; padding-top: 4px;border: silver 1px solid;"> <div id="codeSnippet" style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ColumnDefinition</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="*"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid.ColumnDefinitions</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateManager.VisualStateGroups</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Checked"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">DoubleAnimation</span> <span style="color: #ff0000;">Duration</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">To</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetProperty</span><span style="color: #0000ff;">="(UIElement.Opacity)"</span> <span style="color: #ff0000;">Storyboard</span>.<span style="color: #ff0000;">TargetName</span><span style="color: #0000ff;">="CheckIcon"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Storyboard</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Unchecked"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateManager.VisualStateGroups</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">Stroke</span><span style="color: #0000ff;">="{TemplateBinding BorderBrush}"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="{TemplateBinding Background}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="2"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="2"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Rectangle</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="CheckIcon"</span> <span style="color: #ff0000;">Fill</span><span style="color: #0000ff;">="Orange"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="1,1,0,1"</span> <span style="color: #ff0000;">Stretch</span><span style="color: #0000ff;">="Fill"</span> <span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">="0"</span> <span style="color: #ff0000;">RadiusX</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">RadiusY</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ContentPresenter</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="contentPresenter"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="3"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Grid</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; overflow-x: visible; overflow-y: visible; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">ControlTemplate</span><span style="color: #0000ff;">></span></pre> </div> </div> </div> <p style="text-align: justify;">As you can see I've added an animation, for the Checked state, that animates the Opacity property of the Orange rectangle in the middle of the checkbox . I have not added an animation to the Unchecked state and this mean the VSM will make the transition from Opacity = 1.0 to Opacity = 0.0 when the control move to the uncheked state. If you run the sample you will see the orange rectangle appear when you hit the control.</p> <h4 style="text-align: justify;">Good to know but...</h4> <p style="text-align: justify;">What I've described in these rows is for sure very interesting for people that need a deep customization of controls appearance. But probably, you are now concerned about the complex syntax you have to use and learn. The good news is that Blend, since the release 3.0, is your friend when editing templates. When you are developing an application the better is having a tools that supports template editing. Blend is very powerful and let you manage the constituting elements of the templates but also the states in a very simple way. Learning about how to customize the templates manually is really interesting, but it is only the starting point for the next step: build your own templated controls.</p> http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx editorial@silverlightshow.net (Andrea Boschin ) http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx#comments http://www.silverlightshow.net/items/Understanding-control-customization-with-templates-part-1.aspx Mon, 12 Jul 2010 02:31:00 GMT Create a Custom Control - Inheriting from TextBox <h3><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></h3> <div><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong><br /> </strong></span></em></div> <h3>Introduction</h3> <p>When a control <em>almost</em> does what you want it to – if only it had another button or behaved slightly differently – you may be able to extend it by writing a custom control. Custom controls let you change an existing control or write a completely new control.</p> <p>In this article I am going to describe the steps to extend an existing control. It will inherit from a TextBox and add a watermark and a button to clear existing text.</p> <p>Here is the final result being used as a filter for a data grid, I’ve wired up the WatermarkedTextBox to act as a filter for the items in the list. The “Styled” Checkbox will apply a new theme to the app, including the WatermarkedTextBox:</p> <div style="text-align: center;"><iframe src="http://www.silverlightshow.net/Storage/demos/CustomControlDemo/Default.html" style="width: 406px; height: 406px;"></iframe></div> <p>You can grab the sample project, including the source code for the control, <a href="http://www.silverlightshow.net/Storage/Sources/CustomControlDemo.zip" target="_blank">here</a>. To compile it you need to either have Blend 4 installed, or Visual Studio 2010 with the free Blend 4 SDK.</p> <h3>UserControl vs. Custom Control</h3> <p>UserControls are great when you need a quick way to group some related controls together, such as an address form, or a grid-based browser with navigation controls. Sometimes they come in handy when an existing control <em>almost</em> does what you need it to but just needs a little enhancing. There are <a href="http://silverscratch.blogspot.com/2010/03/pros-and-cons-of-usercontrols.html" target="_blank">advantages and disadvantages</a> of using a UserControl and when the disadvantages start to outweigh the advantages it’s time to look at a <strong>custom control</strong>.</p> <h3>Step 1 – Create the Projects</h3> <p><a href="http://www.silverlightshow.net/Storage/Users/PhilM/EditInVisualStudio_2.png"><img width="172" height="163" title="EditInVisualStudio" style="border:0px solid; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 10px; display: inline; float: right;" alt="EditInVisualStudio" src="http://www.silverlightshow.net/Storage/Users/PhilM/EditInVisualStudio_thumb.png" /></a> Start a new project in Expression Blend (you can also create it in Visual Studio). Select a “Silverlight Application + Website” project type and give it a name. This is going to be the test-bed for the new control.</p> <p>At this point it’s easier to create the control library in Visual Studio. Blend is not designed for serious code entry and it doesn’t help us create any custom visual elements for our control.</p> <p>Right click the main solution at the top of the “Projects” tab and select “Edit in Visual Studio”. Once in Visual Studio, right-click the main solution and select “Add” and “New Project…”. Choose the “Silverlight Class Library” project type and give it a name – this will be the project containing the new custom control. Visual Studio will create a default class in the new project that you can delete and then right click the new project and select “Add” and “New Item…”. Choose the “Class” item type and give it the name “WatermarkedTextBox”.</p> <h3>Step 2 – Subclass TextBox</h3> <p>The new control will subclass TextBox. It will also have a default style, referenced in the constructor like this:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> WatermarkedTextBox : TextBox</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #008000;">/// <summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #008000;">/// Initializes a new instance of the <see cref="WatermarkedTextBox"/> class.</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #008000;">/// </summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> WatermarkedTextBox()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(WatermarkedTextBox);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> <p>The code above tells Silverlight to look for a style that uses TargetType=”WatermarkedTextBox” for this control. It will look for the default style in a specific folder in the project named “Themes”. Right-click the project containing the WatermarkedTextBox class and select “Add” and “New Folder”; name the folder “Themes”. Now add the resource library by right-clicking the “Themes” folder, selecting “Add” and “New Item…”. Choose the “Silverlight Resource Dictionary” item type and name it “generic.xaml”. All Silverlight control libraries must have their default styles defined in a resource dictionary file called “generic.xaml” in a folder called “Themes”.</p> <h3>Step 3 – Create the Default Style</h3> <p>The control needs to have a default style defined in generic.xaml. You can create this by hand, but when you are extending an existing control, it is much easier to start with the default style from the control you are inheriting from. The easiest way to get the default style from the inherited TextBox class is to do the following:</p> <ol> <li>swap back to Blend and, in MainPage.xaml, create a new TextBox on the design surface. </li> <li>reset any properties that Blend changed when you added it. </li> <li>right-click the TextBox and select “Edit Template” and “Edit a Copy”. </li> <li>choose “Apply to all” rather than giving it a Name (key), and choose “This document” for the “Define in” option </li> </ol> <p>Now switch to XAML view for MainPage.xaml and you will see, in the Resources section of the UserControl, a control template with the key “ValidationToolTemplate” and a style with the TargetType set to “TextBox”. Copy this XAML and paste it, back in Visual Studio, into the generic.xaml resource dictionary.</p> <p>The Resource dictionary needs a namespace declaration for the local control like this:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">ResourceDictionary</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns:x</span><span style="color: #0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns:local</span><span style="color: #0000ff;">="clr-namespace:CustomControls"</span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #ff0000;">xmlns:sys</span><span style="color: #0000ff;">="clr-namespace:System;assembly=mscorlib"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>The “local” allows you to reference the new control and the “sys” allows you to reference types in the System namespace in the core (which we will be using later). Change the TargetType from “TextBox” to “local:WatermarkedTextBox”:</p> <p><Style TargetType="local:WatermarkedTextBox"></p> <p>We now have our WatermarkedTextBox at the same level as the basic TextBox. We could compile it now and use it in our project, but it wouldn’t behave any differently from a regular TextBox.</p> <h3>Step 4 – Add Custom Properties</h3> <p>Now we start creating the custom functionality. In this case, for the WatermarkedTextBox, I am going to add the following properties:</p> <ul> <li>Watermark : the text that displays when the TextBox is empty. </li> <li>WatermarkStyle : a Style that will be applied to the Watermark </li> <li>WatermarkForeground : quick access to the brush used for the Watermark. This will override the Foreground specified in the WatermarkStyle property. </li> <li>TextRemoverStyle : a Style that will be applied to the button that clears any entered text </li> <li>TextRemoverToolTip : text for a tooltip for the text removal button </li> <li>IsUpdateImmediate : a Boolean property (which requires the system namespace reference in the XAML) that, if set to True, will cause any Binding source on the WatermarkTextBox.Text property to be updated whenever the text changes, rather than just when the control loses focus (which is the default behavior for the TextBox). This is especially useful if the control is used as a filter for a collection of items. </li> </ul> <p>I added the WatermarkForeground property as a quicker way to set the foreground brush for the watermark text rather than having to edit the WatermarkStyle, and to use as an example of precedence in TemplateBinding default XAML when I get to it.</p> <p>Custom properties don’t have to be dependency properties, but it is a good idea to use dependency properties if you want them to support binding. In the downloadable sample project, I have created a dependency property and a class property for each of the above. Here is the code for the Watermark property:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> WatermarkedTextBox : TextBox</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #008000;">/// <summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #008000;">/// Watermark text dependency property</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #008000;">/// </summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty WatermarkProperty = DependencyProperty.Register(</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #006080;">"Watermark"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">string</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(WatermarkedTextBox),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;">new</span> PropertyMetadata(<span style="color: #006080;">"Enter text here"</span>));</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #008000;">/// <summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #008000;">/// Gets or sets the watermark.</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #008000;">/// </summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #008000;">/// <value>The watermark.</value></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> [Description(<span style="color: #006080;">"Gets or sets the watermark"</span>)]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> [Category(<span style="color: #006080;">"Watermark"</span>)]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Watermark</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> get { <span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">string</span>)GetValue(WatermarkProperty); }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> set { SetValue(WatermarkProperty, <span style="color: #0000ff;">value</span>); }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span>...</pre> <!--CRLF--></div> </div> <p>There are some important points to note about this code:<a href="http://www.silverlightshow.net/Storage/Users/PhilM/Watermark%20Category.png"><img width="244" height="139" title="Watermark Category" style="border:0px solid; margin-top: 25px; margin-right: 0px; margin-bottom: 10px; margin-left: 10px; display: inline; float: right;" alt="Watermark Category" src="http://www.silverlightshow.net/Storage/Users/PhilM/Watermark%20Category_thumb.png" /></a> </p> <ul> <li>The default value for the dependency property (“Enter text here”) should have a matching setter in the default style XAML. In this case, the style has a setter for the Watermark property that matches the default value given in the PropertyMetadata for the WatermarkProperty. </li> <li>The Description attribute (in the System.ComponentModel namespace) is picked up by Blend and used as a tooltip for the property. </li> <li>The Category attribute is used by Blend to create a new grouping category on the Properties tab, shown in the image to the right. </li> <li>The accessors for the Watermark property do nothing other than get/set the value of the dependency property. </li> </ul> <p>The other properties all follow the same pattern.</p> <h3>Step 5 – Add Custom Elements</h3> <p>In this step we add any extra visual elements to the control template in the default style XAML. For this control I’ll add a TextBlock for the watermark and a Button for the text-remover:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">TextBlock</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="Watermark"</span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{TemplateBinding WatermarkStyle}"</span> <span style="color: #ff0000;">Foreground</span><span style="color: #0000ff;">="{TemplateBinding WatermarkForeground}"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">="{TemplateBinding Watermark}"</span> <span style="color: #ff0000;">IsHitTestVisible</span><span style="color: #0000ff;">="False"</span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">ColumnSpan</span><span style="color: #0000ff;">="2"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="TextRemover"</span> <span style="color: #ff0000;">Height</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">HorizontalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">Margin</span><span style="color: #0000ff;">="0,3,3,3"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{TemplateBinding TextRemoverStyle}"</span> <span style="color: #ff0000;">VerticalAlignment</span><span style="color: #0000ff;">="Center"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="14"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Button"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #ff0000;">Grid</span>.<span style="color: #ff0000;">Column</span><span style="color: #0000ff;">="1"</span> <span style="color: #ff0000;">IsTabStop</span><span style="color: #0000ff;">="False"</span> <span style="color: #ff0000;">RenderTransformOrigin</span><span style="color: #0000ff;">="0.5,0.5"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #ff0000;">ToolTipService</span>.<span style="color: #ff0000;">ToolTip</span><span style="color: #0000ff;">="{TemplateBinding TextRemoverToolTip}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">Button</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Use “{TemplateBinding …}” to bind properties on the visual elements to the properties on the control class. In the XAML above you can see that I’ve bound both the Style and the Foreground properties of the TextBlock. As mentioned earlier, the Foreground is there for convenience and will override the Foreground brush specified in the style.</p> <p>The default WatermarkStyle is defined in the XAML as a normal Setter like this:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="WatermarkStyle"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="TextBlock"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="HorizontalAlignment"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Left"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Margin"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="3,0,0,0"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="VerticalAlignment"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Center"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontStyle"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Italic"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="TextWrapping"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="NoWrap"</span> <span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>The TextRemoverStyle is defined similarly, except it has a control template as well. To create the control template for the TextRemoverStyle, I created a button back in Blend, customized the template and style to achieve the look I was after and then copied the XAML from that button, and pasted it into the control template for the TextRemoverStyle.</p> <h3>Step 6 – Add Custom States</h3> <p>This control is going to have two new state groups:</p> <ul> <li>WatermarkStates : A state for when the watermark is visible and a state for when it is hidden </li> <li>TextRemoverStates: A state for when the text-remover button is visible and a state for when it is hidden. </li> </ul> <p>For now we will just add empty place holders in the XAML:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="TextRemoverStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="TextRemoverVisible"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="TextRemoverHidden"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">VisualStateGroup</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="WatermarkStates"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="WatermarkVisible"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <span style="color: #0000ff;"><</span><span style="color: #800000;">VisualState</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="WatermarkHidden"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;"></</span><span style="color: #800000;">VisualState</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">VisualStateGroup</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>These are placed in the same location as the inherited state groups for the TextBox. It isn’t very easy to design good looking states and transitions in XAML by hand, so we will come back to this at the end and use Blend.</p> <p>Blend will know what to do with our states if we decorate our class with the TemplateVisualState attribute for each state we added. We can also use the TemplatePart attribute to let Blend know that our control expects specific visual elements to work with. The StyleTypedProperty lets Blend know the target type of the two style properties we added:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span>[StyleTypedProperty(Property = <span style="color: #006080;">"TextRemoverStyle"</span>, StyleTargetType = <span style="color: #0000ff;">typeof</span>(Button)),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> StyleTypedProperty(Property = <span style="color: #006080;">"WatermarkStyle"</span>, StyleTargetType = <span style="color: #0000ff;">typeof</span>(TextBlock)),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> TemplatePart(Name = <span style="color: #006080;">"TextRemover"</span>, Type = <span style="color: #0000ff;">typeof</span>(Button)),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> TemplatePart(Name = <span style="color: #006080;">"Watermark"</span>, Type = <span style="color: #0000ff;">typeof</span>(TextBlock)),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> TemplateVisualState(Name = <span style="color: #006080;">"WatermarkVisible"</span>, GroupName = <span style="color: #006080;">"WatermarkStates"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> TemplateVisualState(Name = <span style="color: #006080;">"WatermarkHidden"</span>, GroupName = <span style="color: #006080;">"WatermarkStates"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> TemplateVisualState(Name = <span style="color: #006080;">"TextRemoverVisible"</span>, GroupName = <span style="color: #006080;">"TextRemoverStates"</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> TemplateVisualState(Name = <span style="color: #006080;">"TextRemoverHidden"</span>, GroupName = <span style="color: #006080;">"TextRemoverStates"</span>)]</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span><span> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> WatermarkedTextBox : TextBox</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #606060;"> </span><span style="color: black;"> {</span></pre> <!--CRLF--></div> </div> <h3>Step 7 – Add Custom Behavior</h3> <p>There are a few things left to do to make our code and the new visual elements work together. The OnApplyTemplate method must be overridden to wire up the text remover button:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnApplyTemplate()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">base</span>.OnApplyTemplate();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #008000;">// remove old button handler</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != <span style="color: #0000ff;">this</span>.textRemoverButton)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.textRemoverButton.Click -= <span style="color: #0000ff;">this</span>.TextRemoverClick;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #008000;">// add new button handler</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.textRemoverButton = GetTemplateChild(<span style="color: #006080;">"TextRemover"</span>) <span style="color: #0000ff;">as</span> Button;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != <span style="color: #0000ff;">this</span>.textRemoverButton)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.textRemoverButton.Click += <span style="color: #0000ff;">this</span>.TextRemoverClick;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.UpdateState();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> <p>And event handlers need to be hooked up in the constructor for events that are important to this control:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> WatermarkedTextBox()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> DefaultStyleKey = <span style="color: #0000ff;">typeof</span>(WatermarkedTextBox);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.GotFocus += <span style="color: #0000ff;">this</span>.TextBoxGotFocus;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.LostFocus += <span style="color: #0000ff;">this</span>.TextBoxLostFocus;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.TextChanged += <span style="color: #0000ff;">this</span>.TextBoxTextChanged;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span>}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> TextBoxGotFocus(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"WatermarkHidden"</span>, <span style="color: #0000ff;">false</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.isFocused = <span style="color: #0000ff;">true</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.UpdateState();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span>}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> TextBoxLostFocus(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.isFocused = <span style="color: #0000ff;">false</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.UpdateState();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span>}</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> TextBoxTextChanged(<span style="color: #0000ff;">object</span> sender, TextChangedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> <span style="color: #0000ff;">this</span>.UpdateState();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">this</span>.IsUpdateImmediate)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> <span style="color: #0000ff;">return</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span>  </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum32" style="color: #606060;"> </span> BindingExpression binding = <span style="color: #0000ff;">this</span>.GetBindingExpression(TextBox.TextProperty);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum33" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != binding)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum34" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum35" style="color: #606060;"> </span> binding.UpdateSource();</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum36" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum37" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> <p>The “UpdateSource” method causes the control to transition correctly to the appropriate visual state:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> UpdateState()</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">string</span>.IsNullOrEmpty(<span style="color: #0000ff;">this</span>.Text))</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"TextRemoverHidden"</span>, <span style="color: #0000ff;">true</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">this</span>.isFocused)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"WatermarkVisible"</span>, <span style="color: #0000ff;">true</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"TextRemoverVisible"</span>, <span style="color: #0000ff;">true</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> VisualStateManager.GoToState(<span style="color: #0000ff;">this</span>, <span style="color: #006080;">"WatermarkHidden"</span>, <span style="color: #0000ff;">false</span>);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span>}</pre> <!--CRLF--></div> </div> <h3>Step 8 – Define Default Visual States</h3> <p>The last thing to do is to define the visual changes in the states for the controls. The easiest way to do this is to follow these steps:</p> <ol> <li>get the control to the point where it compiles. </li> <li>go into Blend and create an instance of the control on the design surface and customize the template for the created instance. </li> <li>make the changes to each element in the template for each state </li> <li>view the XAML and copy-paste the TextRemoverStates and the WatermarkStates into the appropriate section in generic.xaml. </li> </ol> <p>That pasted XAML now becomes part of the default template for the control when you rebuild it.</p> <h3>Summary</h3> <p>In this article I described in 8 steps how to extend an existing TextBox control into a WatermarkedTextBox custom control. I showed how you can use Blend to extract the default styles and control templates from an existing control and how to add your own visual elements in generic.xaml and wire them up to the new type to create a new custom control.</p> <p><strong>References:</strong> <br /> Sample Data: <a href="http://msdn.microsoft.com/en-us/library/ms762271(v=VS.85).aspx" target="_blank">MSXML SDK</a>. Artwork <a href="http://www.sxc.hu/photo/1259888" target="_blank">one</a>, <a href="http://www.sxc.hu/photo/1259887" target="_blank">two</a>, <a href="http://www.sxc.hu/photo/1278060/" target="_blank">three</a>.</p> http://www.silverlightshow.net/items/Create-a-Custom-Control-Inheriting-from-TextBox.aspx editorial@silverlightshow.net (Phil Middlemiss ) http://www.silverlightshow.net/items/Create-a-Custom-Control-Inheriting-from-TextBox.aspx#comments http://www.silverlightshow.net/items/Create-a-Custom-Control-Inheriting-from-TextBox.aspx Wed, 30 Jun 2010 05:26:00 GMT Separating Content and Presentation with the ContentControl <h3><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></h3> <div><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong><br /> </strong></span></em></div> <h3>Introduction</h3> <p>The ContentControl is often overlooked when building Silverlight apps. It’s used inside many controls such as the Button or ChildWindow, but it also turns out to be quite useful on it’s own for separating content from presentation.</p> <p>Here is a sample of the technique I will be describing. The buttons swap between two completely different “themes” for the same UserControl but the XAML for the UserControl doesn’t contain any theme elements at all, just layout and content. It’s not Picasso, but it will do to illustrate the concept:</p> <div style="text-align: center;"><iframe src="http://www.silverlightshow.net/Storage/demos/SeparatingContentAndPresentation/Default.html" style="width: 600px; height: 400px;"></iframe></div> <p>You can grab the source files <a href="http://www.silverlightshow.net/Storage/Sources/ContentControlTemplates.zip">here</a>.</p> <p>This is what the layout looks like:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/PhilM/ViewLayout_2.png"><img width="595" height="384" title="ViewLayout" style="border:0px solid; display: block; float: none; margin-left: auto; margin-right: auto;" alt="ViewLayout" src="http://www.silverlightshow.net/Storage/Users/PhilM/ViewLayout_thumb.png" /></a> </p> <p>I should point out that the concept I am describing is <strong>not</strong> how to switch from one theme to another (I’m using quick and dirty code to do that). Rather, it is about organizing your XAML files to keep them free from borders and rectangles etc that are purely presentation.</p> <p>In the layout screenshot above, you can see there are no theme-specific elements in the object tree. The child UserControls (“LeftButtons” etc) also just contain content such as the Buttons and the ScrollViewer. So where are the theme elements? They are in the styles for the ContentControls; in this case in App.xaml, but they could easily be placed in a resource dictionary. The XAML for the page is now clean and easy to maintain since it doesn’t have the clutter of all the decorative elements. I can quickly see it and understand it and I can work with the whole Object Tree expanded in Blend.</p> <h3>Layout and Content vs. Presentation</h3> <p>It has always been good practice in web applications to use styles to keep presentation separate from the layout and content of the information. CSS files provide that function for HTML pages, but CSS tends to use lots of images for backgrounds and buttons, whereas Silverlight lets us define most/all of our presentation in XAML. This article describes a way to do, in part, for XAML what CSS did for HTML. It’s not always easy or practical to try and remove the presentation from content, but the kind of situations where this works best are:</p> <p> </p> <ul> <li>Title bars </li> <li>Main application background </li> <li>Tool bars </li> <li>Reusable views such as log-in screens </li> <li>Sidebars </li> <li>Footers </li> </ul> <p>They are patterns where there is content inside a decorative border, and prime targets for using a ContentControl style template to remove the decorative elements of the presentation from the content.</p> <h3>Step 0</h3> <p>This is not some geeky reference to 0-based indexes, rather it’s something you should do <em>before</em> you start throwing controls and resource dictionaries at a project. Stop and take a moment to design your overall application layout. This will usually end up with a group of areas of the screen defined for content such as “Title”, “Menu/Navigation”, “Side Navigation”, “Footer” etc. These may be suitable candidates for ContentControls.</p> <h3>Step 1</h3> <p><a href="http://www.silverlightshow.net/Storage/Users/PhilM/FindingTheContentControl_2.png"><img width="244" height="139" title="FindingTheContentControl" style="border:0px solid; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 10px; display: inline; float: right;" alt="FindingTheContentControl" src="http://www.silverlightshow.net/Storage/Users/PhilM/FindingTheContentControl_thumb.png" /></a> The ContentControl is not immediately visible from the Controls toolbar. The easiest way to find it is to open up the Assets fly-out at the bottom of the Controls toolbar and use the search box in the fly-out to type “ContentControl”. Once you select it you can either draw it on the design surface, or double click the button that is now on the Controls toolbar to place it as a child control of the currently selected element.</p> <h3>Step 2</h3> <p><a href="http://www.silverlightshow.net/Storage/Users/PhilM/CreateStyleResource.png"><img width="244" height="138" title="CreateStyleResource" style="border:0px solid; margin-top: 0px; margin-right: 10px; margin-bottom: 10px; margin-left: 0px; display: inline; float: left;" alt="CreateStyleResource" src="http://www.silverlightshow.net/Storage/Users/PhilM/CreateStyleResource_thumb.png" /></a> Right click the ContentControl and select “Edit Template”, and “Edit a Copy…”. You will be prompted to create a new “Style Resource”, as shown in the image to the left.</p> <p>You have three options for where to store the style resource:</p> <table border="0" cellspacing="0" cellpadding="2"> <tbody> <tr> <td valign="top"> <ul> <li><strong>Application</strong> – will be stored in the App.xaml file. </li> <li><strong>This document</strong> – will be stored in a Resources section within the current UserControl </li> <li><strong>Resource Dictionary</strong> – will be stored in a resource dictionary in a separate file, either in the current project, or another assembly that is part of the solution. </li> </ul> </td> </tr> </tbody> </table> <p>Ultimately, you want the style in it’s own resource dictionary, but quite often it is easier, from a design point of view, to create the style resource in the same document as the ContentControl, and then move it to a resource dictionary when you have finished editing the template. This is because saving the style resource in another file takes it “out of context” when editing, so you are making your changes without seeing it in the page with the actual content it will contain.</p> <h3>Step 3</h3> <p><a href="http://www.silverlightshow.net/Storage/Users/PhilM/BasicContentControlTemplate.png"><img width="306" height="141" title="BasicContentControlTemplate" style="border:0px solid; margin-top: 0px; margin-right: 10px; margin-bottom: 10px; margin-left: 0px; display: inline; float: left;" alt="BasicContentControlTemplate" src="http://www.silverlightshow.net/Storage/Users/PhilM/BasicContentControlTemplate_thumb.png" /></a> Place your ContentPresenter control. The default control template for a ContentControl is a single ContentPresenter. You can place whatever controls you like in this template, as long as you have at least one ContentPresenter. If you are placing the ContentPresenter beneath other elements, be sure to uncheck “IsHitTestVisible” on any that will cover parts of the content that the user will need to interact with. If the template is for a title or footer, this may not be necessary.</p> <h3>The Benefits</h3> <p>I’ve briefly mentioned a couple of reasons that this can be a helpful practice, but it’s worth looking at the benefits in more detail.</p> <h4>Cleaner XAML</h4> <p>Sometimes you need to work with the XAML, rather than Blend, to use language features that Blend just doesn’t support such as custom attached properties. Keeping the UserControls free from all the presentation elements makes for a XAML file that is quick to browse and change.</p> <h4>Reusable Resources</h4> <p>If you organize your resource dictionaries carefully you will end up with visual designs that can be reused again and again. This is especially true if you are creating a suite of applications for your business or client. The resource dictionaries can be kept in their own assemblies and managed separately by your designer or UI developer/team.</p> <h4>Different Themes, Same Application</h4> <p>A recent project I worked on required a single Silverlight application to show very different themes for different customers. Not just a difference of color or size, but large differences such as different logos, animations, and graphics. All customer-specific theme content had to live in its own resource dictionary. I could detect which theme should be applied at application start-up and choose the appropriate resource dictionary to load into the Application resources, but still needed a way to keep each user control free of customer-specific content.</p> <p>We use the Prism MVVM framework, so I could just create different views for each customer, but that would be a big hassle to maintain, and feels wrong since each view does exactly the same thing.</p> <p>I solved this problem by wrapping the main layout areas inside ContentControls and customizing the style control templates for them. In the sample at the top of the page I have just put all the styles into App.xaml and have named the styles for each theme differently, but in the real-life project, I used a different technique described <a href="http://silverscratch.blogspot.com/2010/04/changing-data-templates-at-run-time.html">here</a> to load in the right resources dictionary when the application started up. In that approach the resource dictionaries all have the same style keys since only one dictionary will be used.</p> <h3>Summary</h3> <p>In this post I have described how to separate layout and content from theme elements using ContentControls, and I listed the kinds of areas that lend themselves to this approach:</p> <ul> <li>Title bars </li> <li>Main application background </li> <li>Tool bars </li> <li>Reusable views such as log-in screens </li> <li>Sidebars </li> <li>Footers </li> </ul> <p> </p> <p>Finally I described how to apply the technique, and discussed the benefits which are: having cleaner XAML, generating reusable resources, and using different themes within the same application.</p> http://www.silverlightshow.net/items/Separating-Content-and-Presentation-with-the-ContentControl.aspx editorial@silverlightshow.net (Phil Middlemiss ) http://www.silverlightshow.net/items/Separating-Content-and-Presentation-with-the-ContentControl.aspx#comments http://www.silverlightshow.net/items/Separating-Content-and-Presentation-with-the-ContentControl.aspx Wed, 26 May 2010 07:35:00 GMT Using Silverlight Grid Control <h2><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></h2> <div><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong><br /> </strong></span></em></div> <h2>Introduction</h2> <p>The Grid control is the most flexible layout panel, and supports arranging controls in multi-row and multi-column layouts. It is conceptually similar to an HTML Table element.</p> <p>Grids (not to be confused with DataGrids) offer easy and exact placement by providing a table-like structure. You declare rows and columns, and then place controls into a specific row/column location (spanning across rows or columns as needed).</p> <p>Fortunately, if you're coming from a Web development background, you'll find Silverlight's Grid control easy to use and quick to comprehend because it's similar to what you've already been using. It acts much like HTML's table tag and allows data and controls to be arranged in a tabular-style view.</p> <h2>Overview</h2> <p>The Grid control allows rows and columns to be defined much more concisely compared to HTML. With the HTML table tag, you're forced to repeat multiple tr and td tags to create rows and columns. The Grid control allows rows and column information to be defined in one location using RowDefinition and ColumnDefinition tags.</p> <p>For example, we could declare a Grid layout has three rows and three columns using XAML like below: </p> <p><span style="color: #800000;"><Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True"> <br />         <Grid.RowDefinitions> <br />             <RowDefinition></RowDefinition> <br />             <RowDefinition></RowDefinition> <br />             <RowDefinition></RowDefinition> <br />         </Grid.RowDefinitions> <br />         <Grid.ColumnDefinitions> <br />             <ColumnDefinition></ColumnDefinition> <br />             <ColumnDefinition></ColumnDefinition> <br />             <ColumnDefinition></ColumnDefinition> <br />         </Grid.ColumnDefinitions> <br /> </Grid></span></p> <p>This example sets the Grid's <strong>ShowGridLines</strong> attribute to True, which is nice for seeing the initial layout of a Grid control. We define the Rows for a table inside the Grid.<strong>RowDefinitions</strong> element. For each Row, we add a <strong>RowDefinition</strong> element and we define the Columns for a table inside the Grid.<strong>ColumnDefinitions</strong> element. For each Column, we add a <strong>ColumnDefinition</strong> element</p> <p>The result of the Xaml code is shown in</p> <p><a href="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample1.jpg"><img width="673" height="476" title="sample1" style="border:0px solid;width: 633px; display: inline; height: 393px;" alt="sample1" src="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample1_thumb.jpg" /></a> </p> <h3>Defining Column Width, and Row Height</h3> <p>To provide the most flexibility, Grid columns and rows are sized by GridLength objects which use the GridUnitType, which in turn allows you to choose among: </p> <ul> <li>Auto (size based on the size properties of the object being placed in the grid) </li> <li>Pixel (size in pixels) </li> <li>Star (size based on a weighted proportion of the available space) </li> </ul> <p>Omitting the <em>width</em> and/or <em>height</em> properties makes all columns and rows the same size</p> <p>To see the effect of all this we'll modify the previous example We'll then fill our grid with controls and take a look at some of the effects. </p> <p><span style="color: #800000;"><Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True"> <br />         <Grid.RowDefinitions> <br />             <RowDefinition Height="Auto"></RowDefinition> <br />             <RowDefinition Height="100"></RowDefinition> <br />             <RowDefinition Height="*"></RowDefinition> <br />         </Grid.RowDefinitions> <br />         <Grid.ColumnDefinitions> <br />             <ColumnDefinition></ColumnDefinition> <br />             <ColumnDefinition></ColumnDefinition> <br />             <ColumnDefinition></ColumnDefinition> <br />         </Grid.ColumnDefinitions> <br />         <Button Content="Auto Size" Height="150" Grid.Row="0" Grid.Column="0" ></Button> <br />         <Button Content="Pixel Size" Height="150" Grid.Row="1" Grid.Column="1"></Button> <br />         <Button Content="Star Size" Height="150" Grid.Row="2" Grid.Column="2"></Button> <br /> </Grid></span></p> <p>The result of the Xaml code is shown in</p> <p><a href="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample2.jpg"><img title="sample2" style="border:0px solid;width: 633px; display: inline; height: 393px;" alt="sample2" src="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample2_thumb.jpg" /></a></p> <p>As described above, the RowDefinitions define three rows. The first row will size itself to the TextBlock placed in the row. The second has a fixed height of 100. The  third will take all the remaining space. When you use the * character to define a row height, you're essentially telling the Grid to assign the row 100 percent of the remaining space. You could also omit the Height attribute entirely in this case and achieve the same effect.</p> <p>Now we need The second and third to be float, but in a 3:4 proportion with the remaining space, So we will change the RowDefinition of the tow rows to be like:</p> <p><span style="color: #800000;"><RowDefinition Height="30*"></RowDefinition> <br /> <RowDefinition Height="40*"></RowDefinition></span></p> <p>The result of Xaml Code is shown in</p> <p><a href="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample3.jpg"><img title="sample3" style="border:0px solid;width: 633px; display: inline; height: 393px;" alt="sample3" src="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample3_thumb.jpg" /></a> </p> <p>Instead of setting the exact <em>width</em> for every column and <em>height</em> for every row you can use the alternative <em>MinWidth</em> / <em>MaxWidth</em> and <em>MinHeight</em> / <em>MaxHeight</em> properties.</p> <h3>Placing Controls within The grid</h3> <p>The controls can be placed inside a Grid using Grid.Row and Grid.Column attributes. In situations where you need a control to span multiple rows or columns, you can use the Grid.RowSpan or Grid.ColumnSpan attributes. For example, the following button would be placed in the first row and span two columns:</p> <p><span style="color: #800000;"><Button Content="Auto Size" Height="150" Width="300" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" ></Button></span></p> <p>The result will be</p> <p><a href="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample4.jpg"><img title="sample4" style="border:0px solid;width: 633px; display: inline; height: 393px;" alt="sample4" src="http://www.silverlightshow.net/Storage/Users/basem_gomaa/sample4_thumb.jpg" /></a> </p> <h2>References</h2> <p><a href="http://silverlight4tutorials.blogspot.com/2010/04/silverlight-layout-controls.html">Silverlight Layout Controls</a></p> <h2>Silverlight Books</h2> <p><a href="http://www.payforcompleteorder.com/Books/Silverlight%204%20Lab%20Intensive%20Skills%20Training_12.html">Silverlight 4 Lab Intensive Skills Training</a></p> <p><a href="http://www.payforcompleteorder.com/Books/Professional%20Visual%20Studio%202010%20(Paperback)_11.html">Professional Visual Studio 2010 (Paperback)</a></p> <p><a href="http://www.payforcompleteorder.com/Books/C%20Sharp%204.0%20in%20a%20Nutshell_10.html">C Sharp 4.0 in a Nutshell</a></p> <h2>Additional Resources</h2> <p><a href="http://astore.amazon.com/payforcomplet-20">Silverlight Books</a></p> http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx editorial@silverlightshow.net (Bassem Gomaa ) http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx#comments http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx Wed, 05 May 2010 08:05:00 GMT Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it <h3><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong>This article is compatible with the latest version of Silverlight.</strong></span></em></h3> <div><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><strong><br /> </strong></span></em></div> <h3>Introduction</h3> <p>One of the things I found frustrating in the previous versions of Silverlight was the lack of the Right Mouse Click support. Now we know that this functionality has turned real with Silverlight 4. Furthermore, we know that the April 2010 Silverlight toolkit release comes with a ContextMenu Control to be used in combination with the right click. So it’s time to put these new things in practice and try to use them in our projects. In this article we will see how to create and use a ContextMenu, how it is made and then we will extend it in a peculiar way. <br /> You can download the source code used in the article from <a href="http://www.silverlightshow.net/Storage/Sources/CustomMenuItem.zip" target="_blank">here</a>.</p> <h3>Features included in this release of the ContextMenu Control</h3> <p>As you may already know, the ContextMenu is essentially a menu which appears on a UI control after clicking the right mouse button. This menu typically contains items related to the context of the control and comes in handy in various situations. As written in the <a href="http://blogs.msdn.com/delay/archive/2010/04/15/alive-and-kickin-new-silverlight-4-toolkit-released-with-today-s-silverlight-4-rtw.aspx">Delay’s Blog</a> this release of ContextMenu is a subset of its WPF’s cousin. What is not supported is principally the multi level menu; however, the rest is more or less there. You have obviously mouse and keyboard support, custom positioning of the menu, icon support, enable/disable items and so on. </p> <p> </p> <h3>How to use the ContextMenu Control</h3> <p>First of all you have to download and install the April 2010 release of the Silverlight Toolkit available on <a href="http://silverlight.codeplex.com/releases/view/43528">Codeplex</a>.  After that, add the necessary references (System.Windows.Controls.Toolkit and System.Windows.Controls.Input.Toolkit) and move to your xaml page where you simply have to add to the list of namespace mappings what follows:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; height: 54px; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span>xmlns:toolkit=”http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit”</pre> <!--CRLF--></div> </div> <p>By the way, this is a small but appreciated new entry of the April toolkit release. Now you are ready to attach your first ContextMenu to one of the controls of your UI. If you are already familiar with the WPF version of the ContextMenu you should not have any difficulty in following the rest of the paragraph. Suppose you want to add it to a button: what you have to do is first use an attached property, the so called <em>ContextMenuService. ContextMenu,</em> inside the xaml of the button as described (let’s leave aside what is in the middle for now, we will find it out soon):</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><Button Content=<span style="color: #006080;">"Right click on me"</span> Name=<span style="color: #006080;">"MyButton"</span> Height=<span style="color: #006080;">"23"</span> Width=<span style="color: #006080;">"75"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> ....</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> </toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span></Button></pre> </div> </div> <p>This attached property is defined as follows:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty ContextMenuProperty = DependencyProperty.RegisterAttached(</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <span style="color: #006080;">"ContextMenu"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(ContextMenu),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(ContextMenuService),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">new</span> PropertyMetadata(<span style="color: #0000ff;">null</span>, OnContextMenuChanged));</pre> <!--CRLF--></div> </div> <p>and it essentially serves to set the owner of the ContextMenu. In fact, if you look at the source code of the Silverlight toolkit where the context menu is dealt with “Owner” is an internal property of ContextMenu and when it is set, also an event handler to the mouse right click on the owner is set. The real popup menu is created within this event handler using the Popup primitive and associating a Canvas to it. Let’s now move on to adding other elements to our xaml example as follows:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><Button x:Name=<span style="color: #006080;">"MyButton"</span> Content=<span style="color: #006080;">"Right click on me!"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 1"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> </toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span></Button></pre> <!--CRLF--></div> </div> <p>Here we have just added the ContextMenu in the attached property and inside the ContextMenu we added two menu items. In the image below you may see the result:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu1_2.jpg"><img width="241" height="97" title="ContextMenu1" style="border:0px solid; display: inline;" alt="ContextMenu1" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu1_thumb.jpg" /></a> </p> <p>the ContextMenu is nothing but a class derived from ItemsControl which is the control generally used to present a collection of items. Also MenuItem derives from ItemsControl, but here the intermediate class in the hierarchy, HeaderedItemsControl, exposes something interesting. HeaderedItemsControl, in fact, contains a heading (or title) and multiple items. What we used in the example above is the Header property of HeaderedItemsControl to label our menu items with a string, but if you look at the definition of the Header:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #008000;">// Summary:</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span><span style="color: #008000;">// Gets or sets the item that labels the control.</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span><span style="color: #008000;">//</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span><span style="color: #008000;">// Returns:</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span><span style="color: #008000;">// The item that labels the control. The default value is null.</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> Header { get; set; }</pre> </div> </div> <p>You will see that it is defined as an object; what does It mean? It means that here you can virtually insert whatever you want, not just a string. We will come back on this later on. Let's move on and let’s add other features to our first ContextMenu:</p> <p> </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span>Button x:Name=<span style="color: #006080;">"MyButton"</span> Content=<span style="color: #006080;">"Right click on me!"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 1"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <toolkit:Separator/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 3"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <Image Source=<span style="color: #006080;">"Paste.png"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> </toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span></Button</pre> </div> </div> <p>Below is how it appears with the additions:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu2_2.jpg"><img width="200" height="110" title="ContextMenu2" style="border:0px solid; display: inline;" alt="ContextMenu2" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu2_thumb.jpg" /></a> </p> <p>Separator is simply a basic control stylized as a line and it is useful to separate different items. The Icon property of the MenuItem class is more interesting; it is strongly recommended to set this property if you want to accompany a menu item with an icon. But take a look at how this property is defined:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #008000;">/// <summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span><span style="color: #008000;">/// Identifies the Icon dependency property.</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span><span style="color: #008000;">/// </summary></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty IconProperty = DependencyProperty.Register(</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #006080;">"Icon"</span>,</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">object</span>),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <span style="color: #0000ff;">typeof</span>(MenuItem),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #0000ff;">new</span> PropertyMetadata(<span style="color: #0000ff;">null</span>));</pre> </div> </div> <p>As you can see it is a simple object just like the “Header” property. So what happens if we swap the content of “Header” and “Icon” of our ContextMenu as in the xaml code below?</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span>toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 1"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <toolkit:Separator/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <toolkit:MenuItem ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <Image Source=<span style="color: #006080;">"save.png"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> </toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #006080;">"menu item 3"</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> </toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span></toolkit:ContextMenuService.ContextMenu></pre> </div> </div> <p>It happens what we expect to happen:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu3_2.jpg"><img width="238" height="113" title="ContextMenu3" style="border:0px solid; display: inline;" alt="ContextMenu3" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu3_thumb.jpg" /></a> </p> <p>In other words, “Icon” and “Header” are two virtually interchangeable properties not considering visual side effects.</p> <h3>How it is made</h3> <p>The ContextMenu consists of a “Grid” type container which includes a Rectangle of 28 pixel in width on the left and an ItemsPresenter.</p> <a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu7_4.jpg"><img width="236" height="120" title="ContextMenu7" style="border:0px solid; display: inline;" alt="ContextMenu7" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu7_thumb_1.jpg" /></a> <p>The Rectangle on the left serves as background for the area in which icons are inserted. The ItemsPresenter is used to display the menu items.</p> <p>Let’s focus on a single menu item now:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu8_2.jpg"><img width="300" height="150" title="ContextMenu8" style="border:0px solid; display: inline;" alt="ContextMenu8" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu8_thumb.jpg" /></a> </p> <p>As you can see in the image above, the MenuItem is made of a grid with 4 columns. The first column is reserved for the Icon object, the second for the vertical separator and the third column contains the Header object. The fourth and last column serves as spacer.</p> <h3>How to abuse it</h3> <p>Let’s try to extend the ContextMenu by adding something that looks like a multilevel menu. The idea is: since we found that we can include whatever we want into the “Header” section of the MenuItem, why not adding another ContextMenu there? <br /> Let’s suppose to add a TextBlock in the header and hook a ContextMenu to it through ContextMenuService as in the following piece of code:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <TextBlock Text=<span style="color: #006080;">"menu item 1"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <toolkit:ContextMenu Margin=<span style="color: #006080;">"8"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu AA"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu AB"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> </toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </TextBlock></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> </toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <toolkit:Separator/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 3"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <Image Source=<span style="color: #006080;">"save.png"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> </toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span></toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--></div> </div> <p>The result would be:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu4_2.jpg"><img width="287" height="138" title="ContextMenu4" style="border:0px solid; display: inline;" alt="ContextMenu4" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu4_thumb.jpg" /></a> </p> <p>Nice, isn't it? Actually, it is not quite as good as it appears because you have to do a right click twice: the first on the button and the second on the item of the ContextMenu and this is not what one would expect. So let’s try another way. Let’s take out the attached property ContextMenuService and look for another mechanism to hook the ContextMenu to a single item. <br /> In the code below I added a StackPanel to the Header of the first MenuItem. I set the “Orientation” as “Horizontal” and I inserted a TextBlock to label the MenuItem and a ContextMenu detached from any Control.</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <StackPanel Orientation=<span style="color: #006080;">"Horizontal"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <TextBlock Text=<span style="color: #006080;">"menu item 1"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <toolkit:ContextMenu Margin=<span style="color: #006080;">"2"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu AA"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu AB"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> </StackPanel></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> </toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <toolkit:Separator/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 3"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <Image Source=<span style="color: #006080;">"save.png"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> </toolkit:MenuItem.Icon></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span></toolkit:ContextMenuService.ContextMenu></pre> </div> </div> <p>The result is:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu5_4.jpg"><img width="302" height="144" title="ContextMenu5" style="border:0px solid; display: inline;" alt="ContextMenu5" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu5_thumb_1.jpg" /></a> </p> <p>As you may see it does not look like a traditional multi-level menu because the main menu incorporates also the sub menu; however in some way it works. You can move the pointer on the single items of the sub menu to highlight them and the underlying item remains highlighted. The problem is that the sub menu does not disappear when you go on another MenuItem of the main menu. To add this functionality you can use the mechanism of the attached properties. </p> <p>The attached property I used, which I called ContextMenuHelper.CheckSubMenu is defined in a ContextMenuHelper static class as follows:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> p</span><span style="color: #0000ff;">ublic</span> <span style="color: #0000ff;">class</span> ContextMenuHelper</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> DependencyProperty CheckSubMenuProperty =</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> DependencyProperty.RegisterAttached(<span style="color: #006080;">"CheckSubMenu"</span>, <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">bool</span>), <span style="color: #0000ff;">typeof</span>(ContextMenuHelper),</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">new</span> PropertyMetadata(CheckSubMenuPropertyChanged));</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> </pre> <!--CRLF--></div> </div> <p>Whenever this property is set by a MenuItem, the event handler CheckSubMenuPropertyChanged is fired. In this event handler whose code you can see below:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> CheckSubMenuPropertyChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> FrameworkElement menuItemWithSubMenu = d <span style="color: #0000ff;">as</span> FrameworkElement;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> ((<span style="color: #0000ff;">bool</span>)e.NewValue)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> menuItemWithSubMenu.GotFocus += <span style="color: #0000ff;">new</span> RoutedEventHandler(menuItemWithSubMenu_GotFocus);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> menuItemWithSubMenu.GotFocus -= <span style="color: #0000ff;">new</span> RoutedEventHandler(menuItemWithSubMenu_GotFocus);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span>}</pre> </div> </div> <p>I added a RoutedEventHandler to the GotFocus event of each MenuItem in which the attached property has been set to “true”. The menuItemWithSubMenu’s RoutedEventHandler below has the logics which permit to visualize or hide the sub menu. </p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> menuItemWithSubMenu_GotFocus(<span style="color: #0000ff;">object</span> sender, RoutedEventArgs e)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span>{</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> FrameworkElement menuItemWithSubMenu = (FrameworkElement)sender;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span>( <span style="color: #0000ff;">null</span> == menuItemWithSubMenu)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <span style="color: #0000ff;">return</span>;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <span style="color: #008000;">// search for subContextMenus inside the menuItem </span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> List<ContextMenu> contextMenusInMenuItemWithSubMenu = menuItemWithSubMenu.GetChildrenByType<ContextMenu>(c => c <span style="color: #0000ff;">is</span> ContextMenu);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <span style="color: #0000ff;">if</span> (contextMenusInMenuItemWithSubMenu.Count > 0)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> contextMenusInMenuItemWithSubMenu[0].Visibility = Visibility.Visible;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> <span style="color: #0000ff;">else</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> ContextMenu MainContextMenu1 = menuItemWithSubMenu.Parent <span style="color: #0000ff;">as</span> ContextMenu;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <span style="color: #008000;">// search for other subContextMenus inside other itemMenus</span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> List<ContextMenu> contextMenusInOtherMenuItems = MainContextMenu1.GetChildrenByType<ContextMenu>(c => c <span style="color: #0000ff;">is</span> ContextMenu);</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <span style="color: #0000ff;">foreach</span> (ContextMenu subCM <span style="color: #0000ff;">in</span> contextMenusInOtherMenuItems)</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> {</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> subCM.Visibility = Visibility.Collapsed;</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> }</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span>}</pre> </div> </div> <p>When a MenuItem receives the focus, a search is performed among the child controls to locate a sub-ContextMenu. If a sub-contextMenu is found then it is made visible. If there are no sub-contextMenu another search is performed, this time starting from the main ContextMenu and each sub-contextMenu found is made invisible.</p> <p>What remains to do in order to make it work is adding this attached property to each MenuItem of the ContextMenu and set it to true as in the xaml code below:</p> <div id="codeSnippetWrapper" style="border:1px solid silver;padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'courier new', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; padding-top: 4px; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum1" style="color: #606060;"> </span><Button Content=<span style="color: #006080;">"Mouse Right click here"</span> Height=<span style="color: #006080;">"23"</span> HorizontalAlignment=<span style="color: #006080;">"Left"</span> Margin=<span style="color: #006080;">"167,109,0,0"</span> Name=<span style="color: #006080;">"button1"</span> VerticalAlignment=<span style="color: #006080;">"Top"</span> Width=<span style="color: #006080;">"164"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum2" style="color: #606060;"> </span> <toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum3" style="color: #606060;"> </span> <toolkit:ContextMenu ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum4" style="color: #606060;"> </span> <toolkit:MenuItem local:ContextMenuHelper.CheckSubMenu=<span style="color: #006080;">"True"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum5" style="color: #606060;"> </span> <toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum6" style="color: #606060;"> </span> <StackPanel Orientation=<span style="color: #006080;">"Horizontal"</span> Background=<span style="color: #006080;">"Transparent"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum7" style="color: #606060;"> </span> <TextBlock>menu item 1</TextBlock></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum8" style="color: #606060;"> </span> <toolkit:ContextMenu Margin=<span style="color: #006080;">"2"</span> Visibility=<span style="color: #006080;">"Collapsed"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum9" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"submenu item 1"</span> local:ContextMenuHelper.CheckSubMenu=<span style="color: #006080;">"True"</span> Click=<span style="color: #006080;">"MenuItem_Click"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum10" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum11" style="color: #606060;"> </span> <toolkit:MenuItem Click=<span style="color: #006080;">"MenuItem_Click_1"</span> Header=<span style="color: #006080;">"submenu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum12" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum13" style="color: #606060;"> </span> </StackPanel></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum14" style="color: #606060;"> </span> </toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum15" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum16" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 2"</span> local:ContextMenuHelper.CheckSubMenu=<span style="color: #006080;">"True"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum17" style="color: #606060;"> </span> <toolkit:MenuItem local:ContextMenuHelper.CheckSubMenu=<span style="color: #006080;">"True"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum18" style="color: #606060;"> </span> <toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum19" style="color: #606060;"> </span> <StackPanel Orientation=<span style="color: #006080;">"Horizontal"</span> Background=<span style="color: #006080;">"Transparent"</span>></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum20" style="color: #606060;"> </span> <TextBlock>menu item 3</TextBlock></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum21" style="color: #606060;"> </span> <toolkit:ContextMenu Margin=<span style="color: #006080;">"2"</span> Visibility=<span style="color: #006080;">"Collapsed"</span> ></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum22" style="color: #606060;"> </span> <toolkit:MenuItem Click=<span style="color: #006080;">"MenuItem_Click"</span> Header=<span style="color: #006080;">"submenu item 1"</span> /></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum23" style="color: #606060;"> </span> <toolkit:MenuItem Click=<span style="color: #006080;">"MenuItem_Click_1"</span> Header=<span style="color: #006080;">"submenu item 2"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum24" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum25" style="color: #606060;"> </span> </StackPanel></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum26" style="color: #606060;"> </span> </toolkit:MenuItem.Header></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum27" style="color: #606060;"> </span> </toolkit:MenuItem></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum28" style="color: #606060;"> </span> <toolkit:MenuItem Header=<span style="color: #006080;">"menu item 4"</span> local:ContextMenuHelper.CheckSubMenu=<span style="color: #006080;">"True"</span>/></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum29" style="color: #606060;"> </span> </toolkit:ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum30" style="color: #606060;"> </span> </toolkit:ContextMenuService.ContextMenu></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span id="lnum31" style="color: #606060;"> </span></Button></pre> </div> </div> <p>With this mechanism you can nest other sub-sub-ContextMenu as in the following image:</p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu6_2.jpg"><img width="535" height="205" title="ContextMenu6" style="border:0px solid; display: inline;" alt="ContextMenu6" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu6_thumb.jpg" /></a> </p> <p>Finding the way to prevent the expansion of the parent ContextMenu is left to the reader. </p> <h3>An application to a real case</h3> <p>I have recently ported an application which I already described in two of my previous articles <a href="http://www.silverlightshow.net/items/How-to-extend-Bing-Maps-Silverlight-with-an-elevation-profile-graph-ndash-Part-1.aspx">here</a> and <a href="http://www.silverlightshow.net/items/How-to-extend-Bing-Maps-Silverlight-with-an-elevation-profile-graph-ndash-second-part.aspx">here</a> to Silverlight 4 and I took the chance to introduce a ContextMenu. In the image below you can see an example of a ContextMenu stylized.</p> <p> </p> <p><a href="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu9_2.jpg"><img width="529" height="452" title="ContextMenu9" style="border:0px solid; display: inline;" alt="ContextMenu9" src="http://www.silverlightshow.net/Storage/Users/walterf/ContextMenu9_thumb.jpg" /></a> </p> <h3>Summary</h3> <p>In this article we have explored in depth the new ContextMenu Control introduced with the April 2010 relase of the Silverlight toolkit. We have learned how to create it and use it and then we have enjoyed trying to extend it in an unusual way.</p> http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx editorial@silverlightshow.net (Walter Ferrari ) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comments http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Wed, 05 May 2010 04:25:00 GMT Make the TreeView control to be MVVM compliant <p style="text-align: justify;"><em>During a recent work I found some trouble working with the TreeView control in a Model-View-ViewModel scenario. As usually happen, the standard controls are designed to work in an event-driven behavior and this non always marries with a correct MVVM implementation. After some work, I found a way to change the TreeView and transform it to be lazy loadable.</em> </p> <p style="text-align: justify;"><a href="http://www.silverlightshow.net/Storage/Sources/SilverlightPlayground.MVVMTreeView.zip">Download Source Code</a></p> <p style="text-align: justify;">The Model-View-ViewModel pattern has been introduced in Silverlight by few time and this platform lacks a complete support to the pattern by the standard controls. In Windows Presentation Foundation there is an extended commanding support since the first releases of the platform that enable handling many behavior of the controls and respond easily to the user interaction. This is not true for Silverlight, where the elective model is event-driven and this often cause the need to write code in the codebehind of the page instead of relying on commands and ViewModel. Also if we can rely on Prism's DelegateCommand class, to create some of the missing commands and enhance the support to the pattern, there are some cases where it is not possible to connect directly a command to an event and catch it in the ViewModel.</p> <p style="text-align: justify;">In a recent work I did, one of this not-unusual cases taken me some time to reach a solution. I was working with a SDK's TreeView control, trying to make it lazy loaded in the ViewModel and also to catch the selection of an item to publish the SelectedItem to other ViewModels.</p> <h3 style="text-align: justify;">Understanding the problem</h3> <p style="text-align: justify;">Before starting to analyze the solutions I will propose, it is better to understand where is the problem I would like to resolve. If you scan the events of a TreeView control you will discover that it miss a series of item-level events. The sole event that have this scope is the SelectedItemChanged event we can use to detect if the user changed the currently selected item.</p> <p style="text-align: justify;">But what if I need to detect when the user expands or collapses a node? The treeview completely miss this kind of events. In an event-driven application is still possible to detect the expand/collapse action because the TreeViewItem class exposes the Expanded and Collapsed event so we can write something like this:</p> <div id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet" style="width: 99.48%; height: 93px;"> <pre class="alt"><span class="lnum" id="lnum1"> </span>TreeView tv = <span class="kwrd">new</span> TreeView();</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>TreeViewItem item = <span class="kwrd">new</span> TreeViewItem();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span>item.Expanded += <span class="kwrd">new</span> RoutedEventHandler(item_Expanded);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span>item.Collapsed += <span class="kwrd">new</span> RoutedEventHandler(item_Collapsed);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span>tv.Items.Add(item);</pre> <!--CRLF--></div> </div> <p style="text-align: justify;">This solution besides being complicated is also invalid in a MVVM application because violate a key concept of separation of concerns between the user interface and the Business logic. Using this tecnique we will have to attach every single item of the TreeView to an event handler to catch the actions and this has to be done into the codebehind of the page.</p> <p style="text-align: justify;">So, the problem is that we cannot gain access to the events using DataBinding, because the TreeViewItem class is only a wrapper that is generated by the DataBinding but is not exposed by a template or some properties of the control. This apply not only to the Expanded and Collapsed events, but also to the mouse events, like MouseLeftButtonUp and other else and virtually every other item-level event.</p> <h3 style="text-align: justify;">Anatomy of a TreeView</h3> <p style="text-align: justify;">Similar to many other controls that generates its content from the data source, the TreeView inherits his behavior from the ItemsControl. This flexible control - you may use directly into the xaml or indirectly with some major controls - is substantially capable of taking the elements of a list, attached to the ItemsSource property and apply a template to generate some UI elements. When you use the ItemsControl as base of TreeView (or also of a ListBox), the control wraps every element of the source collection with a specialized item that is responsible to handle item-specific properties and behaviors. </p> <p style="text-align: justify;">The TreeViewItem class is generated by the TreeView control and - as we said in the previous paragraph - it contains some key events like Expanded and Collapsed but also is a subclass of ItemsControl itself. This is the way the TreeView shows a hierarchical structure because every item in the control is binded to the list of children that the item must expose to continue the hierarchy.</p> <p style="text-align: justify;">The interesting part of this structure is that we can change the container class overriding a method of the ItemsControl. The ItemsControl contains some methods we can use to manage the lifetime of the container: </p> <p style="text-align: justify;">1) PrepareContainerForItemOverride() <br /> 2) ClearContainerForItemOverride() <br /> 3) GetContainerForItemOverride() </p> <h3 style="text-align: justify;">Solving the problem</h3> <p style="text-align: justify;">Now we know all the required elements to solve the problem I've illustrated previously. What we would like to do is catch an event, as an example the Expanded event, and bubble it to the main TreeView control that finally will raises and event. This let us creating a specific command and bind it to the ItemExpanded event. <br /> To do this we will create a new LazyTreeView control inherited by the TreeView and then we override the GetContainerForItemOverride method to issue another kind of wrapper. Here is the simplified code:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> LazyTreeView : TreeView</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler ItemExpanded;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler ItemClicked;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> DependencyObject GetContainerForItemOverride()</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> LazyTreeViewItem item = <span class="kwrd">new</span> LazyTreeViewItem();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> <span class="rem">// Expanded</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> item.ItemExpanded += (s, e) => <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemExpanded, s);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="rem">// Clicked</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> item.ItemClicked += (s, e) => <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemClicked, s);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> <span class="kwrd">return</span> item;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span> <span class="kwrd">private</span> <span class="kwrd">void</span> RaiseEvent(EventHandler handler, <span class="kwrd">object</span> sender)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum21"> </span> handler(sender, EventArgs.Empty);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum22"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum23"> </span>}</pre> <!--CRLF--></div> </div> <p style="text-align: justify;">In the GetContainerForItemOverride method we did two things. First of all we instantiate a LazyTreeViewItem class as wrapper. Then we attach the Expanded event to raise the ItemExpanded on the TreeView. Of course while the TreeView is gerarchical this code simple handles the event of the first level but we can create an item-specific class to attach the other levels:</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> LazyTreeViewItem : TreeViewItem</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler ItemExpanded;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler ItemClicked;</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> DependencyObject GetContainerForItemOverride()</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> LazyTreeViewItem item = <span class="kwrd">new</span> LazyTreeViewItem();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> <span class="rem">// Expanded</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> item.ItemExpanded += (s, e) => <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemExpanded, s);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="rem">// Clicked</span></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> item.ItemClicked += (s, e) => <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemClicked, s);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum15"> </span> <span class="kwrd">return</span> item;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum16"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum17"> </span>  </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum18"> </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnExpanded(RoutedEventArgs e)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum19"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum20"> </span> <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemExpanded, <span class="kwrd">this</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum21"> </span> <span class="kwrd">base</span>.OnExpanded(e);</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum22"> </span> }</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum23"> </span> </pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum24"> </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnMouseLeftButtonUp(MouseButtonEventArgs e)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum25"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum26"> </span> <span class="kwrd">this</span>.RaiseEvent(<span class="kwrd">this</span>.ItemClicked, <span class="kwrd">this</span>);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum27"> </span> e.Handled = <span class="kwrd">true</span>;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum28"> </span> <span class="kwrd">base</span>.OnMouseLeftButtonUp(e);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum29"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum30"> </span>  </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum31"> </span> <span class="kwrd">private</span> <span class="kwrd">void</span> RaiseEvent(EventHandler handler, <span class="kwrd">object</span> sender)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum32"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum33"> </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum34"> </span> handler(sender, EventArgs.Empty);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum35"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum36"> </span> }</pre> <!--CRLF--></div> </div> <p style="text-align: justify;">In this snippet I show the LazyTreeViewItem class. It is almost similar to the previous snippet but adds the override of OnExpanded method. It is required to catch the Expanded event of the item and start the bubbling. When the user expand an item the event is catched and an ItemExpanded event is raised. It then start navigating the hierarchy while it arrives to the LazyTreeView.</p> <h3 style="text-align: justify;">Use of the new control</h3> <p style="text-align: justify;">Now that we have prepared our pretty new LazyTreeView we can try to use it to make a lazy-loaded TreeView. In the example attached to this article I use a Ria Service to get files and directories from the server filesystem and populates the nodes on demand when the user expand an element. In the xaml I use the LazyTreeView with a HierarchicalDataTemplate to the data source bind hierarchically.</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><code:LazyTreeView ItemsSource=<span class="str">"{Binding Root}"</span></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> code:ItemExpanded.Command=<span class="str">"{Binding ItemExpandedCommand}"</span>></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <code:LazyTreeView.ItemTemplate></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> <sw:HierarchicalDataTemplate ItemsSource=<span class="str">"{Binding Children}"</span>></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> <TextBlock Text=<span class="str">"{Binding PayLoad.Name}"</span> /></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> </sw:HierarchicalDataTemplate></pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> </code:LazyTreeView.ItemTemplate></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span></code:LazyTreeView></pre> <!--CRLF--></div> </div> <p style="text-align: justify;">As you can see the ItemExpanded event is binded to a command of the ViewModel. When the command is raised the ViewModel is responsible of connecting to the Ria Services and get the content of the expanded node. The command behavior I used to create the ItemExpanded command automatically assign the source element to the CommandParameter so the destination command gets a reference to the expanded element and its ViewModel. (Remember I use Prism to do Model-View-ViewModel programming)</p> <div class="csharpcode-wrapper" id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">public</span> <span class="kwrd">class</span> ItemClickedCommandBehavior : CommandBehaviorBase<LazyTreeView></pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span>{</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum3"> </span> <span class="kwrd">public</span> ItemClickedCommandBehavior(LazyTreeView targetObject)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum4"> </span> : <span class="kwrd">base</span>(targetObject)</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum5"> </span> {</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum6"> </span> targetObject.ItemClicked += <span class="kwrd">new</span> EventHandler(targetObject_ItemClicked);</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum7"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum8"> </span> </pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum9"> </span> <span class="kwrd">void</span> targetObject_ItemClicked(<span class="kwrd">object</span> sender, EventArgs e)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum10"> </span> {</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum11"> </span> <span class="kwrd">base</span>.CommandParameter = ((FrameworkElement)sender).DataContext;</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum12"> </span> <span class="kwrd">base</span>.ExecuteCommand();</pre> <!--CRLF--> <pre class="alt"><span class="lnum" id="lnum13"> </span> }</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum14"> </span>}</pre> <!--CRLF--></div> </div> <p style="text-align: justify;">When the ViewModel receive the ItemExpanded command and gets the elements from the filesystem it wraps them with a HierarchicalViewModel. This class contains the item and adds it a Children collection and a IsLoaded property. This property is useful to avoid duplicated loading of the elements. After the items has been loaded the flag is set to true and then none will try to load it again.</p> <p style="text-align: justify;">So the trick is done but we have another little thing to notice. When the ViewModel creates the HierarchicalViewModel instances it is required to load every instance with a dummy child instance. The scope of this instance is to show the expansion cross that is displayed only if a node has at least one child. I load every item with a dummy FileSystemItem with the Text set to "loading...". This cause the user to see this message when it open the node and then it is replaced by the content coming from the server. </p> <div id="codeSnippetWrapper"> <div class="csharpcode" id="codeSnippet" style="width: 98.96%; height: 45px;"> <pre class="alt"><span class="lnum" id="lnum1"> </span><span class="kwrd">if</span> (item.Type == FileSystemItemType.Folder)</pre> <!--CRLF--> <pre class="alteven"><span class="lnum" id="lnum2"> </span> vm.Children.Add(<span class="kwrd">new</span> HierarchicalViewModel<FileSystemItem>(<span class="kwrd">new</span> FileSystemItem { Name = <span class="str">"loading..."</span> }));</pre> <!--CRLF--></div> </div> <h3 style="text-align: justify;">Recap</h3> <p style="text-align: justify;">When you work with the Model-View-ViewModel pattern you must have always in mind that not always you can apply the pattern correctly if you are using the standard controls. The controls are designed to work in an event-driven world so they may require to write some code in the codebehind. It isn't not a bad thing but for sure modifying the controls to make them compliant with the pattern is often possible and let you have more reusable and tested code.</p> http://www.silverlightshow.net/items/Make-the-TreeView-control-to-be-MVVM-compliant.aspx editorial@silverlightshow.net (Andrea Boschin ) http://www.silverlightshow.net/items/Make-the-TreeView-control-to-be-MVVM-compliant.aspx#comments http://www.silverlightshow.net/items/Make-the-TreeView-control-to-be-MVVM-compliant.aspx Tue, 05 Jan 2010 04:52:00 GMT Implicit Styles in Silverlight <h3></h3> <h3><strong><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;">This article is compatible with the latest version of Silverlight.</span></em><br /> </strong></h3> <div><strong><em><span style="font-size: 11pt; line-height: 115%; font-family: calibri, sans-serif;"><br /> </span></em></strong></div> <h3><strong>Introduction</strong></h3> <p>Only three months after the release of the latest official version of Silverlight 3, a new beta version – Silverlight 4 is already a fact. There are a lot of new things, which deserve to be mentioned, such as Rich Text, drop target, webcam, microphone, etc. Check out the <a href="http://www.silverlight.net/getstarted/silverlight-4/#whatsnew">official Silverlight site</a> for more information. However, in this article I decided to show you one very interesting feature, which is taken from WPF, namely it is the <strong>implicit styles</strong> feature.<br /> What was the situation till now? Whenever you’ve created a style  in Silverlight, you were obligated to specify the <strong>TargetType </strong>as well as an unique <strong>Key/Name</strong> for the style. You should also explicitly set the <strong>Style</strong> property of the desired element.</p> <div id="codeSnippetWrapper" style="border:1px solid silver;background-color: #f4f4f4; font-family: 'courier new', courier, monospace; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="ButtonStyle"</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Background"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Red"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Foreground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Black"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontSize"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="16"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Height"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="55"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Width"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="140"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Margin"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="8"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="RenderTransformOrigin"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="0.5,0.5"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="RenderTransform"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RotateTransform</span> <span style="color: #ff0000;">Angle</span><span style="color: #0000ff;">="-15"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">.................</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button1"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="First Button"</span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{StaticResource ButtonStyle}"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button2"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second Button"</span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{StaticResource ButtonStyle}"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>This is also known as a <em>named style</em>.</p> <p style="text-align: center;"> <img alt="" src="http://www.silverlightshow.net/Storage/Users/ppopadiyn/Implicit%20Stlyes%20in%20Silverlight%204/ImplicitStyles-010.png" /></p> <h3> <p><span style="font-size: 16px;">But not any more! Silverlight 4 gives you the ability to create implicit styles.</span></p> <strong>Creating Implicit Styles</strong></h3> <p>If you omit the <strong>Style</strong>’s <strong>Key</strong> and specify only the <strong>TargetType</strong>, then the <strong>Style </strong>is automatically applied to all elements of that target type within the same scope (it is <strong>implicitly applied</strong>). This is typically called a <em>typed style</em> as opposed to a <em>named style</em>, which is the only kind of <strong>Style </strong>we’ve seen so far.</p> <p style="text-align: center;"><img alt="" src="http://www.silverlightshow.net/Storage/Users/ppopadiyn/Implicit%20Stlyes%20in%20Silverlight%204/ImplicitStyles-020.png" /></p> <p>The following example demonstrates you how to create an implicit style, which will be applied to all <strong>Buttons</strong> in the <strong>Application</strong> scope.</p> <div id="codeSnippetWrapper" style="border:1px solid silver;background-color: #f4f4f4; font-family: 'courier new', courier, monospace; text-align: left;"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Background"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Red"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Foreground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Black"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontSize"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="16"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Height"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="55"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Width"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="140"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Margin"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="8"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="RenderTransformOrigin"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="0.5,0.5"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="RenderTransform"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">RotateTransform</span> <span style="color: #ff0000;">Angle</span><span style="color: #0000ff;">="-15"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter.Value</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"></</span><span style="color: #800000;">Setter</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;">.............</pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button1"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="First Button"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button2"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second Button"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">StackPanel</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p> Note, you shouldn't specify the <strong>Button</strong>'s <strong>Style</strong> property.</p> <p style="text-align: center;"><img alt="" src="http://www.silverlightshow.net/Storage/Users/ppopadiyn/Implicit%20Stlyes%20in%20Silverlight%204/ImplicitStyles-030.png" /> </p> <p>In such an application, all Buttons get this style by default. But each Button can still override its appearance by explicitly setting a different Style or explicitly setting properties.</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button2"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second Button"</span> <span style="color: #ff0000;">Background</span><span style="color: #0000ff;">="Green"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--></div> </div> <p style="text-align: center;"> <img alt="" src="http://www.silverlightshow.net/Storage/Users/ppopadiyn/Implicit%20Stlyes%20in%20Silverlight%204/ImplicitStyles-040.png" /></p> <p>Also, any Button can restore its default <strong>Style </strong>by setting its <strong>Style</strong> property to <strong>null</strong>.</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Button</span> <span style="color: #ff0000;">x:Name</span><span style="color: #0000ff;">="button2"</span> <span style="color: #ff0000;">Content</span><span style="color: #0000ff;">="Second Button"</span> <span style="color: #ff0000;">Style</span><span style="color: #0000ff;">="{x:Null}"</span><span style="color: #0000ff;">/></span></pre> <div style="text-align: center;"><span style="color: #0000ff;"> <p><span><img alt="" src="http://www.silverlightshow.net/Storage/Users/ppopadiyn/Implicit%20Stlyes%20in%20Silverlight%204/ImplicitStyles-050.png" /></span></p> </span><br /> </div> <!--CRLF--></div> </div> <h3><strong>The Target Element Must Match Exactly the TargetType</strong></h3> <p>Note, that the <strong>TargetType</strong> must match exactly for a typed style to be applied. For example, if you specify the <strong>Style</strong>’s <strong>Key</strong>, then it’s ok for the target element to be a subclass of the <strong>TargetType</strong>. But a typed style typically gets applied to elements which type matches exactly! This is done to prevent surprises. For example, you might have created a <strong>Style</strong> for all <strong>ToggleButton</strong>s in your application and you don’t want this style to be applied to any <strong>CheckBoxe</strong>s (which derives from the ToggleButton).</p> <h3></h3> <h3><strong>Where is the Key for the Keyless Resource?</strong></h3> <p>So far so good, but you may wonder how it is possible a <strong>Style</strong> to get away with being a member of a <strong>ResourceDictionary</strong> without having a key. It actually <strong>does</strong> have a key – it’s just set implicitly. And the implicit key is simply the value of the <strong>TargetType </strong>(at least this is the situation in WPF, probably it is also true for Silverlight!?!).</p> <h3><strong>Implicit Styles and BasedOn</strong></h3> <p>If you have multiple implicit styles relating to a particular control, then they do not magically combine. For example, if you have the following two styles:</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontSize"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="16"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Background"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Red"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Foreground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Black"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>they won’t get combined. Something more - this will result in a run-time error. However, implicit styles can still use <strong>BasedOn</strong>. After the refactoring of the previous example it should look like the code below:</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px; text-align: left;border-style: none;"> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">x:Key</span><span style="color: #0000ff;">="BasedStyle"</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="FontSize"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="16"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> </pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"><</span><span style="color: #800000;">Style</span> <span style="color: #ff0000;">TargetType</span><span style="color: #0000ff;">="Button"</span> <span style="color: #ff0000;">BasedOn</span><span style="color: #0000ff;">="{StaticResource BasedStyle}"</span><span style="color: #0000ff;">></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Background"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Red"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"> <span style="color: #0000ff;"><</span><span style="color: #800000;">Setter</span> <span style="color: #ff0000;">Property</span><span style="color: #0000ff;">="Foreground"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">="Black"</span><span style="color: #0000ff;">/></span></pre> <!--CRLF--> <pre style="text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'courier new', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow-x: visible; overflow-y: visible; padding-top: 0px;border-style: none;"><span style="color: #0000ff;"></</span><span style="color: #800000;">Style</span><span style="color: #0000ff;">></span></pre> <!--CRLF--></div> </div> <p>Undoubtedly, this is a great addition to the Silverlight styling. You can define implicit styles at any level of your application. Additionally you have the ability to override at any time the implicit style or even to reset it. </p> http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx editorial@silverlightshow.net (Pencho Popadiyn ) http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx#comments http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx Tue, 24 Nov 2009 07:14:00 GMT