Silverlight Ux Musings: Providing Panning Functionality for a Canvas of Objects – Part 2 [Corrina Barber]

I’m back with part 2 of the blog on panning functionality (part 1 is here), and, to quickly recap, we’re creating a region in a web site that can be panned rather than scrolled (users can click and drag to pan and navigate the region). We’re also designing the region so images always align properly (no images clip when the user finishes panning).

I have a sample solution that looks like my design goal pictured at right that can be downloaded here or you can simply check it out online here. Please note that the UI in this solution has many buttons and features that are disabled because my primary focus was to provide code for the panning functionality.


More detailed information can be found on the Silverlight.NET site

§  Microsoft Silverlight 1.1 Alpha September Refresh

§  Visual Studio 2008

§  Microsoft Silverlight Tools Alpha Refresh for Visual Studio 2008 Beta 2 (July 2007)

§  Expression Blend 2 September Preview

Details: Visual Studio (xaml)

Open the solution created in part 1 (a copy can be found here). Again, this is a wireframe version of only the panning region; not the full fidelity UI pictured above. The first thing we need to do is to make a few modifications to Page.xaml.

1.       Open Page.xaml and find the canvas named ‘allImgs’. Right below the opening tag add the following clipping path xaml. This clipping path is sized the same as the parent canvas (remember, we planned to use the height and width of this canvas as defined in Blend as a guide when creating the clipping path in Visual Studio)


      <RectangleGeometry Rect=0,0,264,356></RectangleGeometry>


2.       Locate the ‘allImgsRectangle’ and add an opacity attribute and set it to 0. Remember, we want to only show this rectangle when the mouse is in the panning region (we could have done this in Blend too ;)).

<Rectangle Width=280 Height=372 Stroke=#FF000000 Canvas.Left=12 Canvas.Top=12 x:Name=allImgsRectangle Opacity=0/>

3.       Locate the ‘allImgsCanvas’ and then locate the <Canvas.RenderTransform> tag for the canvas (it should be right below the opening tag).  Add an x:Name attribute to <TranslateTransform.. and name it ‘allImgsTransform’. This will allow us to control the transform values from code.



                     <ScaleTransform ScaleX=1 ScaleY=1/>

                     <SkewTransform AngleX=0 AngleY=0/>

                     <RotateTransform Angle=0/>

                     <TranslateTransform x:Name=allImgsTransform X=0 Y=0/>



4.       Locate the ‘allImgsPan’ storyboard (it should be at the very top of the page), and then locate the two <DoubleAnimationUsingKeyframe> tags that apply to the TranslateTransform X and Y values. Add x:Name attributes to both child <ChildDoubleKeyFrame> tags as shown below.


              <Storyboard x:Name=allImgsPan>

<DoubleAnimationUsingKeyFrames BeginTime=00:00:00 Storyboard.TargetName=allImgsCanvas Storyboard.TargetProperty=(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)>

<SplineDoubleKeyFrame KeyTime=00:00:00 Value=1/>

<SplineDoubleKeyFrame KeyTime=00:00:00.5000000 Value=1.1/>

<SplineDoubleKeyFrame KeyTime=