How to programmatically set selected Panorama item in WP7

0 votes
asked Jan 7, 2011 by cacuzcatlan

I'm using a panorama control in a WP7 app. One of the PanoramaItems takes you to another page, which then allows you send an email through the EmailComposeTask. If you don't select to send the email and press the back button, the Panorama returns to the item you last selected. However, if you do select to send an email (and therefore leave the app), it does not return to the previously selected PanoramaItem. Instead, it returns to the first item in the Panorama. I tried keeping track of the selected index and setting it, but I got an error saying the SelectedIndex is not settable. This is confirmed on MSDN documentation http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.panorama.selectedindex%28VS.92%29.aspx

Is there any way to manually set the selected index/item on a panorama? If not, is there a way for it to remember what was selected, even if the user leaves the app to compose an email?

5 Answers

0 votes
answered Jan 7, 2011 by mick-n

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.

0 votes
answered Jan 7, 2011 by alastair-pitts

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.

0 votes
answered Jan 12, 2012 by elw00t

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.

0 votes
answered Jan 20, 2012 by truong-hua

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

0 votes
answered Jan 21, 2012 by phil

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!

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...