How to programmatically set selected Panorama item in WP7

Solution 1:

I'm not sure if you can programmatically force an animation to another PanoramaItem, but you can change the Panorama.DefaultItem.

So you might have 3 PanoramaItem's and on the OnNavigatedTo() handler, change the default item via:

panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];

This should help when you recover from a tombstone.

Solution 2:

You could try the solution posted by Silicon Shark in this thread. It's noted to work, but only on the initial display - which shouldn't be a problem for your requirements of restoring state after tombstoning.

How to programmatically set the visible item in a Panorama control?

You can get the currently active page from the panorama's SelectedIndex property.

Unfortunately setting DefualtItem is only an approximation to solving this problem, which you may have discovered already.

Edit: Be aware that setting DefaultItem, changes which page of the panorama is the first page. It's a subtle difference, but you will see how it matters looking at the positioning of the heading and the wrap around of the background image.

Solution 3:

Here is a solution. It does work as expected and does not rearrange your panorama, so your user interface is consistent.

pan.SetValue(Panorama.SelectedItemProperty, panoramaItem);
Panorama temp = pan;
LayoutRoot.Children.Remove(pan);
LayoutRoot.Children.Add(temp);
LayoutRoot.UpdateLayout();

this is not a perfect solution in that it does not slide nicely like panorama should, and it is probably not very efficient, but on the other hand you are not changing the default item so your user interface stays consistent.

Solution 4:

I tested solutions listed here without success. Here is what I did that works like a charm!

PanoramaItem panItem = (PanoramaItem)panorama.Items[1];

panorama.Items.Remove(panItem);

panorama.Items.Insert(0, panItem);

You need to remove the panel from the list and re-inserting it at the desired position!

Solution 5:

Set new selected item by

pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newSelectedItem]);

However, it work only on the initial so my idea is let the panorama control re-init when we change the selected item. This is my code, just add this after Panorama.SelectedItem changing.

(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;

But there is not transition effect now! Although, you can create your self.

It work great for me, this page also create a effect for sliding right http://xme.im/slide-or-change-panorama-selected-item-programatically