in

vbCity Blogs

New (temp) place for vbCity Blogs

CanOz Blog

Neil Knobbe's place for Visual Basic.NET and WPF

August 2009 - Posts

  • Expander Control and Silverlight 3

     For a while now I knew that the WPF Expander control was going to be included for use with Silverlight 3. The official Silverlight site mentioned it more than once.

    I even found some blogs that showed using the Expander with Silverlight, but when the time came to insert an Expander into a new Silverlight project all the good feelings stopped.

    Error and assumption number 1:

    All the new Silverlight controls are not automatically included in the Visual Studio ToolBox.

    I am not sure why it was done this way but they are not.  I could see some of the new controls, like the TabControl, which I also wanted to use but not the Expander.  At the beginning this was not a big problem for me because I had other projects to work on that didn’t require the Expander, but when time came to start the project with it I was in trouble.  I asked a friend about it and he reminded me to try the “Choose ToolBox Items” option which can be found under “Tools” in the main menu of Visual Studio.

    Selecting the Silverlight Components tab on the new window I could choose from 40 Silverlight controls.  Everything I could want from a Border to a ToolTip.

    There was one problem however.  No Expander control.

    I was no further ahead so I did some more investigating.

    Error and assumption number 2:

    Don’t just because it is written doesn’t mean that the item in question won’t change after publication.

    Some things can change in software between builds and versions (beta, SDK, release, Silverlight 2, Silverlight 3 etc) and at some point in time I what I have written here there will be a good chance that I will be caught in the same trap.

    I spent a good amount of time on Google looking for examples of using the Expander control in a Silverlight application and found more than a few examples.  There was one common element which kept me banging my head against the wall.  Most, if not all, the examples showed adding the reference to the System.Windows.Controls library and adding the Namespace to the Silverlight UserControl.

    I had gotten that far as I was also using the TabControl on this particular project.

    <UserControl

       xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

       x:Class="Expander.Page"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       Width="400" Height="300">

     To get access to the Controls in the Namespace I would just type "basics:" in the XAML for the UserControl and Intellisense would show me the Controls available to me.

    Yet again there was no Expander available to me.  I was pretty sure that the Expander was useable with Silverlight 3 as there is Sample Source Code in the Silverlight 3 SDK so it followed, in my mind at least, that it should be accessible in Visual Studio.

    Solution:

    The solution to this was in one way good to get yet in another it brought another minor annoyance with it.

    I re-read what this page on the Silverlight Toolkit on the CodePlex site and noticed a minor but important fact.  The Expander control is not actually in the same Library as the TabControl like I thought when I first read the page.  The TabControl is in the System.Windows.Contols library.  The Expander is in the System.Windows.Controls.Toolkit library.  I missed the “Toolkit” part and quickly realized that I would have to add another reference to the project.

    To add the Reference, I right clicked on References in the Solution Explorer window of the project and from the sub-menu selected “Add Reference”.  From there I clicked the “Browse” button and navigated to the Silverlight SDK directory and dug down in the sub-directories until I found the System.Windows.Controls.Toolkit.dll file, selected the file and added by clicking the “OK” button.

    The next step was to add the Namespace to my project.  There is time when Intellisense is really your friend.
    I started with xmlns:controls= and was presented with a choice to select from.

    Double clicking on the System.Windows.Controls.Toolkit option got Visual Studio to complete the declaration for me and I was ready to use the Expander.

    <UserControl

       xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

       xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"

       x:Class="Expander.Page"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       Width="400" Height="300">

     You would think after all this and getting the Expander into my project that I would finish with a really happy ending but I can’t say that I am 100% satisfied.

    There is still one thing that bothers me but because I now have access to the Expander control I can live with it. 

    The bother?  When I type in the XAML markup for an Expander Intellisense does not show what controls are included in the Toolkit library like the Windows.Controls library does in the image above.

    I did find it rather interesting that it does pick up all the Properties of the Expander once I have typed in what control I am trying to use.

    Like I said it is a small bother but one that I can live with.  At least I have use of control I really want in my project.

Copyright 1998-2009 vbCity.com LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems