0
votes

I have a longlistselector to show a gallery of the projects that I have in my app. Every item of the longlistselector have an image of the project, the name and an image to share it in social networks. The problem is that I need know when I touch the share image to leave to a different page that allow us to share it. This is the gallery longlistselector xaml:

<phone:LongListSelector x:Name="GaleryLongListSelector" SelectionChanged="GaleryLongListSelector_SelectionChanged" Margin="0,0,0,15">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="-20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="100"  toolkit:TiltEffect.IsTiltEnabled="True" Grid.Column="0">
                                <Image Width="80" RenderTransformOrigin="0.5,0.5" Height="80" Source="{Binding ThumbImage}">
                                    <Image.RenderTransform>
                                        <RotateTransform Angle="90"/>
                                    </Image.RenderTransform>
                                </Image>
                                <!--<StackPanel Orientation="Vertical">-->
                                <TextBlock x:Name="txtProjectName" Margin="20,0" VerticalAlignment="Center" Text="{Binding Name}"    Style="{StaticResource PhoneTextNormalStyle}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />
                                    <!--<ScrollViewer  VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible">
                                        <TextBlock x:Name="txtProjectDescript"   Text="Aqui iria una descripcion muy larga del faldksjfjkldjfkldajsfkljaslfkjasldfjlasdjfkl" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                                    </ScrollViewer>-->
                                <!--</StackPanel>-->
                            </StackPanel>
                            <Image Source="/Images/share.png"  Height="50" Tap="Image_Tap" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right"/>
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu IsZoomEnabled="True" x:Name="ContextMenu">
                                    <toolkit:MenuItem  Header="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.MainPagePanoramaItemGalleryContextMenuDelete}" Click="Delete_Click"/>
                                    <toolkit:MenuItem  Header="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.MainPagePanoramaItemGalleryContextMenuRename}" Click="Rename_Click"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                        </Grid>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

I read the item selected in a "SelectionChanged" event like this:

private void GaleryLongListSelector_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (GaleryLongListSelector != null && GaleryLongListSelector.SelectedItem != null)
        {
            var selectedItem = (Project)GaleryLongListSelector.SelectedItem;
            var id = selectedItem.ID;
            NavigationService.Navigate(new Uri("/ProjectViewPage.xaml?projectID=" + id.ToString(), UriKind.Relative));
        }
    }

I can use the tap event of the image but using this method I can't find the index of the longlistselector item touched.

Thanks everyone!!

5

5 Answers

0
votes

Use the IndexOf() method on your ItemsSource colelction..

var dataItems = GaleryLongListSelector.ItemsSource as List<Project>;
var selectedItem = (Project)GaleryLongListSelector.SelectedItem;
var indexOfSelectedItem = dataItems.IndexOf(selectedItem)
0
votes
public partial class MainPage : PhoneApplicationPage
{
    public Product selectedItemData;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        lsttest.ItemsSource = App.lstp;
        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    private void Add_Click_1(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri(@"/Page2.xaml", UriKind.Relative));
    }

    private void lsttest_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        selectedItemData = e.AddedItems[0] as Product;
        NavigationService.Navigate(new Uri(@"/Page2.xaml", UriKind.Relative));
    }

    // Sample code for building a localized ApplicationBar
    //private void BuildLocalizedApplicationBar()
    //{
    //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    //    ApplicationBar = new ApplicationBar();

    //    // Create a new button and set the text value to the localized string from AppResources.
    //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
    //    appBarButton.Text = AppResources.AppBarButtonText;
    //    ApplicationBar.Buttons.Add(appBarButton);

    //    // Create a new menu item with the localized string from AppResources.
    //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    //    ApplicationBar.MenuItems.Add(appBarMenuItem);
    //}
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        Page2 destinationpage = e.Content as Page2;
        if (destinationpage != null)
        {

            // Change property of destination page
            destinationpage.GetProduct = selectedItemData;
        }
    }
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        //Page2 destinationpage = e.Content as Page2;
        //if (destinationpage != null)
        //{

        //    // Change property of destination page
        //    destinationpage.GetProduct = selectedItemData;
        //}
    }
}
0
votes

public partial class Page2 : PhoneApplicationPage { public Product GetProduct { get; set; }

    public Page2()
    {
        InitializeComponent();

    }

    private void Add_Click_1(object sender, RoutedEventArgs e)
    {
        Product p2 = stkProduct.DataContext as  Product;
      //  p2.Name = txtName1.Text;
        if (string.IsNullOrEmpty(p2.ID))
        {
            p2.ID = DateTime.Now.ToString("yyyyMMddhhmmsstt");
            App.lstp.Add(p2);
        }
        else
        {
            int index = App.lstp.IndexOf(p2);
            App.lstp.Remove(GetProduct);
            App.lstp.Insert(index, p2);
        }
        NavigationService.Navigate(new Uri(@"/MainPage.xaml", UriKind.Relative));
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (GetProduct == null)
        {
            GetProduct = new Product();
            ApplicationBarIconButton btn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            btn.Text = "Add";
            btn.IconUri = new Uri("/Assets/check.png", UriKind.Relative);
        }
        else
        {
            ApplicationBarIconButton btn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            btn.Text = "save";
            btn.IconUri = new Uri("/Assets/save.png", UriKind.Relative);
        }
        stkProduct.DataContext = GetProduct;




    }

    private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
    {
        Product p2 = stkProduct.DataContext as Product;
        //  p2.Name = txtName1.Text;
        if (string.IsNullOrEmpty(p2.ID))
        {
            p2.ID = DateTime.Now.ToString("yyyyMMddhhmmsstt");
            App.lstp.Add(p2);
        }
        else
        {
            int index = App.lstp.IndexOf(p2);
            App.lstp.Remove(GetProduct);
            App.lstp.Insert(index, p2);
        }
        NavigationService.Navigate(new Uri(@"/MainPage.xaml", UriKind.Relative));
    }
}
0
votes
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="lsttest" SelectionChanged="lsttest_SelectionChanged_1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    <TextBox x:Name="txtname" Text="{Binding Name}"></TextBox>
                    <TextBox x:Name="txtID" Text="{Binding ID}" Visibility="Collapsed"></TextBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
0
votes
using System;
using System.IO.IsolatedStorage;

namespace MobileCartWL.HelperClasses
{
    public class Helper
    {
        #region appSettings
        public static IsolatedStorageSettings appSettings;
        #endregion

        //This function is used to insert Key,Value pair information in the Isolated Storage memory
        #region InsertDetailInMemory
        public static void InsertDetailInMemory(string key, object value)
        {
            try
            {
                appSettings = IsolatedStorageSettings.ApplicationSettings;
                if (!appSettings.Contains(key))
                    appSettings.Add(key, value);
                else
                    appSettings[key] = value;

                appSettings.Save();
            }
            catch (Exception) { }
        }
        #endregion

        //This function is used to remove Key,Value pair information from the Isolated Storage memory
        #region RemoveDetailInMemory
        public static void RemoveDetailInMemory(string key)
        {
            appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains(key))
            {
                appSettings.Remove(key);
                appSettings.Save();
            }
        }
        #endregion

        //This function is used to check the existing of Key,Value pair information in the Isolated Storage memory
        #region IsExistKeyInMemory
        /// <summary>
        /// Check if Specified Key Is Exists or not
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>true if it exists otherwise return false</returns>
        public static bool IsExistKeyInMemory(string key)
        {
            appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains(key))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        //This function is used to get Key,Value pair information from the Isolated Storage memory
        #region GetDetailFromMemory
        /// <summary>
        /// Get value from memory of Specified Key
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>value as Object of Specified Key</returns>
        public static object GetDetailFromMemory(string key)
        {
            object value = string.Empty;
            try
            {
                appSettings = IsolatedStorageSettings.ApplicationSettings;
                if (appSettings.Contains(key))
                {
                    value = appSettings[key];
                }
                return value;
            }
            catch (Exception)
            {
                return value;
            }
        }
        #endregion
    }
}