Wednesday, 10 March 2010

Flexibox – A Silverlight alternative to Lightbox


Flexibox is an alternative to lightbox, displaying multiple resolutions of an image without needing a popup overlay. Flexibox shows how a Silverlight app can resize itself with a page.

Great photos need to displayed big to be best appreciated, but smaller image sizes work best within a block of text, or as a collection of thumbnails. To solve this design dilemma thumbnail images often contain a link to view a larger version of the same image. If this takes the user to another page, then this is inconvenient as the user has to go back to the original page to continue.

A popular alternative is to use a JavaScript library, such as Lightbox, to display an overlay image on the current page. This is very impressive the first few times one uses it. However the overlay hides the rest of the page. The distraction and time taken acts as a disincentive for users to see the larger version of the image.

Flexibox displays a single image within a HTML page in the same way as an <img> tag. Flexibox can change the image it displays to one of a different size and automatically resizes itself within the page. This enables a user to go from a thumbnail to a large resolution image without needing to leave the page.

The Flexibox appears on a web page just like any thumbnail image, but has an enlarge button overlaid on the top right to indicate to the user there is a larger version.

When the user clicks on the enlarge button the Flexibox is enlarged instantly causing the surrounding page elements to re-formatted to flow around the now larger control.

To see a Flexibox live go to


The Code

The code demonstrates a number of useful Silverlight techniques:
Passing parameters from the HTML page to the Silverlight
Converting CSS color strings into Silverlight Color structures
Loading content relative to the host page
Asynchronous loading and caching of images
Resizing a Silverlight control within a page

For what seems to be a very simple application Flexibox contains a lot more code than can be covered here, so only the techniques listed above are described. The full source code is  provided, so you can study all the details in full.
The project was developed with VS 2010 targeting Silverlight 3.

The first thing to study is the way the Flexibox control HTML differs from the standard.

<div style="margin-right: 8px; margin-bottom: 3px;float: left;">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="240px" height="161px">
  <param name="source" value="ClientBin/FlexiBox.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="3.0.40818.0" />
  <param name="autoUpgrade" value="true" />
  <param name="initParams" value="border=2,border_color=#444,preload=true,mode=small,thumb_url=images/s1_100.jpg,medium_url=images/s1_500.jpg,large_url=images/s1_1024.jpg"  />
  <a href="" style="text-decoration:none">
               <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

The object tag is given a fixed width and height in pixels. These attributes will be later changed from within Silverlight.

The div tag surrounding the object tag uses an in-line style to set the margin and float the control to the left of the surrounding text. You can use almost any style in here, except for  width or height. Also do not set an id or class style for the div tag, as the default Visual Studio generated page will do.

The initParams param tag contains the parameters to be passed to the Silverlight control. The example above sets the width and color of the border and passes in four images. The image URLs are relative to the page, just as one would use in an img tag. The mode is set to small so that the small image will be the first one displayed.

The Flexibox XAML

There is only one control in the application and that is defined as follows.

<Border BorderBrush="{Binding Path=BorderBrush}" BorderThickness="{Binding Path=BorderThickness}" Name="border1"  >
    <Grid x:Name="LayoutRoot" >
        <Image  Name="image1" Stretch="None" Source="{Binding Path=DisplayedImage}" />
        <controlsToolkit:BusyIndicator Height="59" HorizontalAlignment="Center"  Name="busyIndicator1" VerticalAlignment="Center" Width="151" IsBusy="{Binding Path=IsBusy}" BusyContent="{Binding Path=BusyStatus}" DisplayAfter="00:00:02.1000000" IsEnabled="True" />
        <StackPanel Height="25" HorizontalAlignment="Right" Margin="0,2,2,0" Name="stackPanelButtons" VerticalAlignment="Top"  Orientation="Horizontal">
            <Button Style="{StaticResource ButtonIcon}" Click="Contract_Button_Click" Name="ContractButton" RenderTransform="{StaticResource ButtonBottomLeft}" Visibility="{Binding Path=ContractVisible}" />
            <Button Style="{StaticResource ButtonIcon}" Click="Expand_Button_Click" Name="ExpandButton"  RenderTransform="{StaticResource ButtonTopRight}" Visibility="{Binding Path=ExpandVisible}" />

As you would expect there is a Border, an Image control and a Busy Indicator for when  loading the first image.

The StackPanel is used to align the two buttons, which only become visible when they can be used. The buttons are simply re-styled. See the styles.xaml file for details.

All the dynamic elements are controlled by data binding to properties of the model.

The Code behind file

public partial class MainPage : UserControl
    FlexiBoxViewModel viewModel = new FlexiBoxViewModel();
    public MainPage()
        DataContext = viewModel;
    private void Contract_Button_Click(object sender, RoutedEventArgs e)
    private void Expand_Button_Click(object sender, RoutedEventArgs e)

As can be seen all the interesting stuff is in the viewModel. Handlers for the button clicks are required as we are using Silverlight 3. In 4 we can get rid of these and use commands directly within the XAML.

The App.xaml.cs file contains only the generated code, so no need to look at that.

Reading the parameters from the HTML page

foreach (string key in Application.Current.Host.InitParams.Keys)
    ParseExternalParam(key, Application.Current.Host.InitParams[key]);

The Application Host contains a dictionary of all the parameters passed into the control, which can be processed one by one.

private void ParseExternalParam(string key, string value)
        else if (String.Compare("thumb_url", key, StringComparison.InvariantCultureIgnoreCase) == 0)
            images[(int)FlexiImageModel.Size.thumb].Url = value;
        else if (String.Compare("border", key, StringComparison.InvariantCultureIgnoreCase) == 0)
            borderSize = Double.Parse(value);
        else if (String.Compare("border_color", key, StringComparison.InvariantCultureIgnoreCase) == 0)
            borderColor = value.ToColor();
        // contain any exceptions thrown by invalid params

Each parameter key is checked for a match and then the value is converted into the appropriate type and stored for later use.

As elsewhere in the code exceptions are caught and safely ignored.   If this is not done then entering an invalid number for the border parameter would cause a nasty error message to be displayed to the user.

This may be what you want, in which case remove the try catches, but I preferred the app to fail quietly and continue in the same way XAML does when it finds errors.

Unlike the full version of .Net there is no built in way of converting a string representation of a color into a color object in Silverlight.

Many people have had to tackle this in the last two years and there are plenty of examples on the web, but most just do hex strings in one format.

I wanted a color string converter that could handle all the CSS color formats. e.g. '#FFFFFF', '#FFF', 'white'.

So I wrote the following utility class.

public static class TWSilverlightUtilities
    // Converts a CSS style string into Color structure. Returns black if the input is invalid.
    // The color string to convert. May be in one of the following formats; #FFFFFFFF, #FFFFFF, #FFF, white.
    public static Color ToColor(this string str)
        Color rv = Color.FromArgb(0xFF,0,0,0);
            rv = str.ToColorEx();
        return rv;
    public static Color ToColorEx(this string str)
        // empty string check
        if ((str == null) || (str.Length == 0))
            throw new Exception("empty color string");
        // This is the only way to access the colors that XAML knows about!
        String xamlString = "<Canvas xmlns=\"\" Background=\"" + str + "\"/>";
        Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
        SolidColorBrush brush = (SolidColorBrush)c.Background;
        return brush.Color;

I really wanted to write this as an extension method of Color, as it would then match the ToString method nicely.

Unfortunately Color is a struct and extension methods do not work properly on them as the 'this' parameter can only be passed by value and not by reference, which makes it useless for structs. Also you cannot define a static extension method, only instance one. Both serious omissions in the language in my view.

Loading content relative to the host page

The URLs of the images to display could be given as absolute or relative to the web page. The Silverlight control may be hosted in a completely different location so the correct address must be used, which is obtained from HtmlPage.Document.DocumentUri. Its then a matter of stripping off the page name, query and fragment and appending the relative image URL, to give us the absolute URL of the image that is required.

private Uri GetImageUri(FlexiImageModel.Size size)
    Uri imgUri = new Uri(images[(int)size].Url, UriKind.RelativeOrAbsolute);
    if (imgUri.IsAbsoluteUri)
        return imgUri;
    // Make an absolute URL relative to the document we are in
    UriBuilder pageUri = new UriBuilder(HtmlPage.Document.DocumentUri);
    pageUri.Fragment = null;
    pageUri.Query = null;
    int n = pageUri.Path.LastIndexOf('/');
    if (n > 0)
        pageUri.Path = pageUri.Path.Substring(0, n + 1);
    return new Uri(pageUri.Uri, imgUri);

Asynchronous loading and caching of images

Each Flexibox can hold up to four images; thumb, small, medium and large. These can either be loaded one at a time on  request, or all pre-loaded.

The pre-load feature gives the user instant access to the different sized images, but uses more band width, so each option has its uses.

When pre-loading the most important thing is to load the image to be initially shown first. Only when that is download should the other images be downloaded and cached.

It is possible that the user may click on the expand button while the next image is being downloaded and the code needs to cope with that.

The caching of the images is plain C# and not a Silverlight technique, so it is not explained in detail here, but is in the attached source code.

private void RetrieveImage(FlexiImageModel.Size size)
        // Dont start a second retrieve of the same image
        if (!images[(int)size].IsRetrieving)
            Uri uri = GetImageUri(size);
            images[(int)size].IsRetrieving = true;
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(uri, size);
    catch (Exception ex)
        images[(int)size].IsError = true;

To download an image firstly get its full URL as described above and then use a WebClient to start the download asynchronously, so as not to block the user interface.

When starting the operation we pass in the size of the image, so we know which image has been downloaded when it completes.

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    FlexiImageModel.Size size = newSizeMode;
    if (e.UserState is FlexiImageModel.Size)
        size = (FlexiImageModel.Size)e.UserState;
        if ((e.Cancelled) || (e.Error != null))
            throw new Exception("Error downloading image");
        BitmapImage bitmap = new BitmapImage();
        images[(int)size].Bitmap = bitmap;
        images[(int)size].IsError = false;
        images[(int)size].IsError = true;
        BusyStatus = "Error loading image";
    images[(int)size].IsRetrieving = false;

WebClient has several options on how to receive the downloaded data. By using OpenReadAsync it provides an open stream in e.Result, which is perfect to set as the source for the new BitmapImage object.

Of course things can go wrong and any errors are caught and the image marked as an error in the cache.

Once the image has been cached Flexibox is ready to load it into the user control and then start the download of the next image.

Loading an image into the user interface

private void LoadImage()
    if ( ((imageLoaded) && (sizeMode == newSizeMode)) ||
        (images[(int)newSizeMode].IsError) || (!images[(int)newSizeMode].IsLoaded))
        return; // nothing to do
    sizeMode = newSizeMode;
    imageLoaded = true;
    ContractVisible = (sizeMode != NextSmallerImage(sizeMode)) ? Visibility.Visible : Visibility.Collapsed;
    ExpandVisible = (sizeMode != NextBiggerImage(sizeMode)) ? Visibility.Visible : Visibility.Collapsed;
    IsBusy = false;


The method first checks for errors and if the required image is already loaded, before proceeding with the rest of the operation.

The Expand and Contract buttons need to be turned on or off depending on whether there is a bigger or smaller image available.

public Visibility ExpandVisible { get { return expandVisible; } set { expandVisible = value; NotifyPropertyChanged("ExpandVisible"); } }
public Visibility ContractVisible { get { return contractVisible; } set { contractVisible = value; NotifyPropertyChanged("ContractVisible"); } }

This is done by setting the two public properties that the XAML defined buttons are bound to.

public BitmapImage DisplayedImage { get { return images[(int)sizeMode].Bitmap; } }

The XAML defined image is bound to the  DisplayedImage property, which retrieves the image directly from the cache. However the image will not be updated as it needs to be told that the selected image has changed. This is done with the NotifyPropertyChanged call at the exact time required, which is after the control has first been resized to fit the new image.

Resizing a Silverlight control within a page

Now for the final technique and the one that this control is really all about, resizing a Silverlight control within a page.

I want to give full credit for this technique to Charles Petzold.

private void ResizeControl()
    if (!images[(int)sizeMode].IsLoaded)
    double height = Math.Max(20,(2 * borderSize) + images[(int)sizeMode].Bitmap.PixelHeight);
    double width = Math.Max(20,(2 * borderSize) + images[(int)sizeMode].Bitmap.PixelWidth);
    if (controllWidth != width)
        controllWidth = width;
        SetDimensionPixelValue("width", controllWidth);
    if (controllHeight != height)
        controllHeight = height;
        SetDimensionPixelValue("height", controllHeight);

Each bitmap image exposes its dimensions so we can calculate the new width and height of the control by adding these to the border thickness.

It is then a matter of setting the width and height using this method.

void SetDimensionPixelValue(string style, double value)
    HtmlPage.Plugin.SetAttribute(style, ((int)Math.Round(value)).ToString() + "px");

HtmlPage.Plugin provides access to the HtmlElement of the object tag. It is then just a case of setting either the width or the height attribute to a text definition of the measurment.

In order for this to work it is important that the page does not contain additional width and height settings that may affect the control within the page, or one of its div containers. This is done by default using styles in the generated code.


As well as explaining a number of basic Silverlight techniques, I am hoping that the control itself shows how Silverlight can be used as an integral part of a web page design.

Source code

Try it live


  1. 成功多屬於那些很快做出決定,卻又不輕易變更的人。而失敗也經常屬於那些很難做出決定,卻又經常變更的人 ....................................................

  2. 當身處逆境時,要能忍一時之氣,吃一時之苦,並及早確立自己的目標,總有一天,你會嘗到那甘美的果實。

  3. 生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。......................................................................

  4. 我真心在追求我的夢想時,每一天都是繽紛的。因為我知道每一個小時都是實現理想 的一部份。 ....................................................

  5. 當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

  6. 如果成為一支火柴,也要點亮一個短暫的宇宙;如果是一隻烏鴉,也要叫疼閉塞的耳膜。.................................................................

  7. Drive carefully. It is not only cars that can be recalled by their Maker.............................................................

  8. 我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。............................................................

  9. 死亡是悲哀的,但活得不快樂更悲哀。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  10. 知識可以傳授,智慧卻不行。每個人必須成為他自己。............................................................