2

I am programmatically adding items to my Panorama Control called PanoramaCC.

//function to create the panorama items in our view
private void showPanorama(string panoramaName)
{
    //create the panorama item and define it
    PanoramaItem genItem = new PanoramaItem();
    genItem.Height = 265;
    genItem.Width = 440;
    genItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(PanoramaItem_Tap);
    genItem.Name = panoramaName;

    //create the stackpanel for the panoramaitem
    StackPanel genStack = new StackPanel();
    genStack.Orientation = System.Windows.Controls.Orientation.Horizontal;
    //margin to be done
    genStack.Margin = new Thickness(0, -20, 0, 20);

    //load the image
    Image genImg = new Image();
    genImg.Height = 220;
    genImg.Width = 400;
    genImg.Stretch = System.Windows.Media.Stretch.Fill;
    genImg.Margin = new Thickness(20, 5, 20, 5);

    string path = "Assets/AppGraphics/CreditCards/" + panoramaName.ToString() + "Front.png";
    Uri uriR = new Uri(path, UriKind.Relative);
    BitmapImage imgSource = new BitmapImage(uriR);
    genImg.Source = imgSource;

    //add image into stackpanel
    genStack.Children.Add(genImg);
    //add stackpanel to the panoramaitem
    genItem.Content = genStack;
    //add the panoramaitem to the panoramaview
    this.PanoramaCC.Items.Add(genItem);
}

The issue I have is that during runtime I want to retrieve the name of the panoramaItem I am currently looking at and do something with it. I've managed to retrieve the name through the tap event for navigation purposes, string name = ((PanoramaItem)sender).Name; but this is a diffrent scenario. I want to retrieve the name and then delete the item with the corresponding name. Pressing a button should delete the currently selected panoramaItem, is what I'm trying to achieve.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
r wank
  • 352
  • 7
  • 22

1 Answers1

4

You can get the current PanoramaItem by using the SelectedItem property. You don't need to get the name to delete it.

PanoramaItem currentItem = myPanorama.SelectedItem as PanoramaItem;
if(currentItem != null)
{
   //if you want the name for other reasons
   string name = currentItem.Name;

   //Items returns an ItemsCollection object
   myPanorama.Items.Remove(currentItem);       
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • derp im facepalming hard right now. i saw the selectedItem for the panoramaControl but my brain didnt recognize it. Thanks – r wank Mar 22 '13 at 13:45
  • Any idea how I get the PanoramaItem when the SelectedItem is a model class? I just use an ItemTemplate and data binding so my list of model objects is shown as a PanoramaItem which means that SelectedItem as well as Items only refer to my model class(es). Meaning the above code always result in currentItem being null. – WarrenFaith Jun 26 '13 at 15:10
  • I've not tried retrieving the SelectedItem from a panorama item but [this behaviour](http://geoffwebbercross.blogspot.co.uk/2011/12/wp7-mvvm-panorama-selectedindex-binding.html) might help as well as [this workaround](http://stackoverflow.com/questions/14260701/windows-phone-8-panorama-selectionchanged-databinding). – keyboardP Jun 26 '13 at 21:06
  • I already use the workaround you linked. Anyway I created a new [question](http://stackoverflow.com/questions/17340869/get-image-in-currently-selected-panoramaitem), maybe you have time to take a look. – WarrenFaith Jun 27 '13 at 10:36