Question

I'm trying to make my longlistselector to navigate to different pages, according to the option chosen. For example A navigates to a.xaml B navigates to b.xaml, same with C D E etc.

Xaml

<phone:LongListSelector
                SelectionChanged="SelectionChanged"
  x:Name="AddrBook"
  JumpListStyle="{StaticResource AddrBookJumpListStyle}"
  Background="Transparent"
  GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
  ItemTemplate="{StaticResource AddrBookItemTemplate}"
  LayoutMode="List"
  IsGroupingEnabled="true"
  HideEmptyGroups ="true" Foreground="Black" Margin="0,20,0,0" FontSize="32"/>

Code Behind:

{
    public partial class ASectionPopSpanish : PhoneApplicationPage
    {
        public ASectionPopSpanish()
        {
            InitializeComponent();

            List<AddressBook> source = new List<AddressBook>();
            source.Add(new AddressBook("Joe", "Davidbisbal"));
            source.Add(new AddressBook("AdJoe", "Davidbisbal"));
            source.Add(new AddressBook("BJoe", "Davidbisbal"));
            source.Add(new AddressBook("VJoe", "Davidbisbal"));
            source.Add(new AddressBook("VJoe", "Davidbisbal"));
            source.Add(new AddressBook("CJoe", "Davidbisbal"));
            source.Add(new AddressBook("EJoe", "Davidbisbal"));
            source.Add(new AddressBook("Joe", "Davidbisbal"));

            List<AlphaKeyGroup<AddressBook>> DataSource = AlphaKeyGroup<AddressBook>.CreateGroups(source,
                System.Threading.Thread.CurrentThread.CurrentUICulture,
                (AddressBook s) => { return s.LastName; }, true);
            AddrBook.ItemsSource = DataSource;


        }

        public class AddressBook
        {
            public string LastName
            {
                get;
                set;
            }
            public string SectionPage
            {
                get;
                set;
            }

            public AddressBook(string lastname, string sectionpage)
            {
                this.LastName = lastname;
                this.SectionPage = sectionpage;
            }
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                AddressBook selectedAddress = (AddressBook)AddrBook.SelectedItem;
            if (AddrBook.SelectedItem == null)
                return;
            NavigationService.Navigate(new Uri(AddressBook.SelectedItem.SectionPage & ".xaml", UriKind.Relative));
            AddrBook.SelectedItem = null;
            }
    }
}

I understand I need to implement a selectionchanged, but can't figure out how to do it, can someone give me a hand please?

Was it helpful?

Solution

What seems missing from your snippet is, XAML/code to attach selector_SelectionChanged method to SelectionChanged event of LongListSelector :

<phone:LongListSelector
              SelectionChanged="selector_SelectionChanged"
              x:Name="AddrBook"
              .....
              ...../>

In addition, we need to cast LLS's SelectedItem to specific type to be able to access complete set of it's properties :

AddressBook selectedAddress = (AddressBook)AddrBook.SelectedItem;
NavigationService.Navigate(new Uri(selectedAddress.SectionPage + ".xaml", UriKind.Relative));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top