Windows Phone Toolkit LockablePivot in depth

published on: 03/01/2020

by WindowsPhoneGeek

One of the new components which comes with the latest update of the Windows Phone Toolkit - August 2011 (7.1 SDK) is LockablePivot control. Basically, it adds a special mode to the Pivot control where only the current item is shown (locked). Lockable Pivot is suitable in cases when you have multiple selection, sliding, drawing, etc.

In this post I am going to talk about this control in details: all you need to do in order to get started using LockablePivot, detailed example and more.

Getting Started

To begin with using the LockablePivot first add a reference to the Microsoft.Phone.Controls.Toolkit.dllassembly in your Windows Phone application project.

NOTE*:*** The Microsoft.Phone.Controls.Toolkit.dll assembly is installed with the toolkit and you can find it in:

For 32-bit systems:

C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll

For 64-bit systems:

C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll

Alternatively you can select it directly from the "...\Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011\Bin" if you have downloaded the "Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011.zip" instead.

NOTE: If you have installed the toolkit via NuGet then the reference is automatically added to your project.

You can create an instance of the LockablePivot control either in XAML or with C#.

Define an instance of the LockablePivot control in XAML: you have to add the following namespace declaration:

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

<toolkit:LockablePivot x:Name="pivot"> <controls:PivotItem Header="Home"> <!--...--> </controls:PivotItem> </toolkit:LockablePivot>

**NOTE: Make sure that your page declaration includes the "toolkit" namespace! You can do this by using VisualStudio Toolbox, Expression Blend Designer or just include it on your own.

NOTE: PivotItem is placed inside "Microsoft.Phone.Controls" so you will need to add a reference to "Microsoft.Phone.Controls.dll" and after that include the following namespace in XAML: xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"

Define an instance of the LockablePivot control in C#: just add the following using directive:

using Microsoft.Phone.Controls;**

LockablePivot pivot = new LockablePivot(); pivot.Items.Add(new PivotItem() { Header = "Home"});

Key Properties

LockablePivot is in fact a simple control that derives from Pivot, so it exposes all the properties of the Pivot plus one more additional IsLocked property. For more information take a look at the Pivot MSDN Documentation.

IsLocked

IsLocked is a property of type bool. It gets or sets the navigation lock mode of the LockablePivot.

Lock and Unlock LockablePivot example

This example demonstrates how to implement a simple drawing application using the LockablePivot control as drawing surface. Here are the steps you will need to follow:

Step1. Define an instance of the LockablePivot control in XAMl and add the code below inside its items. The most important part here is the drawing canvas. You will also have to subscribe to its MouseMove and MouseLeftButtonDown events.

<toolkit:LockablePivot x:Name="pivot"> <controls:PivotItem Header="Home"> <TextBlock Text="Welcome to My Drawing App!" FontSize="60" Foreground="YellowGreen" TextWrapping="Wrap"/> </controls:PivotItem> <controls:PivotItem Header="Drawing"> <StackPanel> <Button Content="Lock Pivot" Click="btnLock_Click"/> <TextBlock Text="Drawing area here:"/> <Canvas x:Name="ContentPanelCanvas" Background="LightGray" Height="300" Width="480" MouseMove="ContentPanelCanvas_MouseMove" MouseLeftButtonDown="ContentPanelCanvas_MouseLeftButtonDown"/> <Button Content="UnLock Pivot" Click="btnUnLock_Click"/> </StackPanel> </controls:PivotItem> </toolkit:LockablePivot>

NOTE*:*** PivotItem is placed inside "Microsoft.Phone.Controls" so you will need to add a reference to "Microsoft.Phone.Controls.dll" and after that include the following namespace in XAML: xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls".

Step2. Define the drawing logic in MouseMove and MouseLeftButtonDown handlers of the drawing Canvas.

public partial class MainPage : PhoneApplicationPage { private Point currentPoint; private Point oldPoint; public MainPage() { InitializeComponent(); } private void ContentPanelCanvas_MouseMove(object sender, MouseEventArgs e) { currentPoint = e.GetPosition(this.ContentPanelCanvas); Line line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y }; line.Stroke = new SolidColorBrush(Colors.Red); line.StrokeThickness = 15; this.ContentPanelCanvas.Children.Add(line); oldPoint = currentPoint; } private void ContentPanelCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { currentPoint = e.GetPosition(ContentPanelCanvas); oldPoint = currentPoint; } //... }

Step3. Add the following code in, to Lock/Unlock the pivot.

private void btnLock_Click(object sender, RoutedEventArgs e) { this.pivot.IsLocked = true; } private void btnUnLock_Click(object sender, RoutedEventArgs e) { this.pivot.IsLocked = false; }

Step4. Here is the result:

As you can see every time when you want to draw on the drawing canvas you can press "Lock Pivot" to lock the current pivot item. When the drawing is ready you can press "Unlock Pivot" button to unlock the pivot item and navigate to the next/previous one.

That was all about the new "LockablePivot" from the Windows Phone Toolkit Aug 2011 in depth. Stay tuned for more Windows Phone Toolkit articles. Here is the full source code:

LockablePivotSample

You may also find interesting the following articles:

You can also follow us on Twitter: @winphonegeek for Windows Phone; @winrtgeek for Windows 8 / WinRT

Comments

Disable Pivot swipe gesture posted by: Josephe Garinii on 09/02/2011 18:03:17 I was trying to disable Pivot swipe gesture so that I am able to perform sliding inside Pivot Item. I used Touch.FrameReported event and also ManipulationStarted of the slider. However it seems that this LockablePivot can do exactly what I was trying to implement. Thanks for the post guys.I was about to miss this control from the toolkit package :)

InvalidCastException posted by: swinglow on 09/19/2012 09:37:13 hi wpg, when I try to set the IsLocked property to true I get an InvalidCastException. have you seen that? here is my xaml: <toolkit:LockablePivot x:Name="pivot" Title="MY APPLICATION" ItemsSource="{Binding RouterKegCollection}"> <controls:Pivot.HeaderTemplate> <DataTemplate> <Grid x:Name="grid"> <TextBlock Text="{Binding RouteName}" /> </Grid> </DataTemplate> </controls:Pivot.HeaderTemplate> <controls:Pivot.ItemTemplate> <DataTemplate > <StackPanel> <userControls:RouterKegQuickStats /> <ListBox ItemsSource="{Binding SessionsOC}" > <ListBox.ItemTemplate> <DataTemplate> <userControls:SessionItem /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </DataTemplate> </controls:Pivot.ItemTemplate> </toolkit:LockablePivot> thanks! I love your site