locked
how to use navigation parameter using Listbox SelectedIndex? RRS feed

  • Question

  • I bind an items to a listbox;

    here's my code MainPage.xaml.cs;

    private void display_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {          
                 Frame.Navigate(typeof(SecondPage), display.SelectedIndex);
    
            }

    In the SecondPage.cs I bind the same items but in different layout;
    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                string u = e.Parameter.ToString();
    
                int y = Convert.ToInt32(u);
                display1.SelectedIndex = y;
            }
    
            private void Page_Loaded(object sender, RoutedEventArgs e)
            {
    
                
            }

    Still there's an error..

    Friday, January 9, 2015 11:08 AM

Answers

  • what error you have? how are the items lost in the listbox?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by icce cage Saturday, January 10, 2015 5:21 PM
    Friday, January 9, 2015 12:08 PM
  • Hello icce cage,

    Can you tell us which error do you get? I tried your code myself and it worked fine.
    Maybe the Problem is that the ListBox "display1" doesn't have so many ListBoxItem as "display" so that the SelectedIndex can not be set with the parameter.
    You can first check if the SelectedIndex can be set with "y" and then set it.

    Regards,
    Bo Liu
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    • Marked as answer by icce cage Saturday, January 10, 2015 5:14 PM
    Friday, January 9, 2015 12:18 PM
    • Marked as answer by icce cage Saturday, January 10, 2015 5:20 PM
    Friday, January 9, 2015 10:54 PM
  • You are developing on RT. You should download my app(https://code.msdn.microsoft.com/Music-online-and-18b2e6d2/sourcecode?fileId=116978&pathId=1927464242) and find  the files name is " common" . Inside of files have 

    -NavigationHelper.cs

    -ObservableDictionary.cs

    -RelayCommand.cs

    -SuspensionManager.cs

    and you add folder to your app.

    the first pages:

    add this code and Note :

    xmlns:common="using:App3.Common" 
    <Page 
        x:Class="App3.BlankPage1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="using:App3" 
        xmlns:common="using:App3.Common" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" Background="White" 
        > 

    and inside of ".cs"

    using App3.Common;

    and

       private void lst_singer_SelectionChanged(object sender, SelectionChangedEventArgs e) 
            { 
                
                if(lst_us.SelectedIndex!=-1) 
                { 
                    //background 
     
                                
                     
                        try 
                        { 
                             
                            string value = (lst_us.SelectedItem as Data.Data).name; 
                            this.Frame.Navigate(typeof(BlankPage1), value); 
                        } 
                        catch (Exception ex) 
                        { 
                            //txtnotice.Text = ex.Message; 
                        } 
                } 
            } 

    The second pages:

    Next steps.

     private NavigationHelper navigationhelper; 
     private ObservableDictionary deafaultviewmodel = new ObservableDictionary(); 

     public BlankPage1() 
            { 
                this.InitializeComponent(); 
                this.navigationhelper = new NavigationHelper(this); 
                this.navigationhelper.LoadState += navigationhelper_LoadState; 
                this.navigationhelper.SaveState += navigationhelper_SaveState; 
                //pivot 
               
                 
            } 

     public NavigationHelper NavigationHelper 
            { 
                get { return this.navigationhelper; } 
            } 
     
            public ObservableDictionary DefaultViewModel 
            { 
                get { return this.deafaultviewmodel; } 
            } 
            protected override void OnNavigatedFrom(NavigationEventArgs e) 
            { 
                this.navigationhelper.OnNavigatedFrom(e); 
            } 
     
            /// <summary> 
            /// Invoked when this page is about to be displayed in a Frame. 
            /// </summary> 
            /// <param name="e">Event data that describes how this page was reached. 
            /// This parameter is typically used to configure the page.</param> 
            protected override void OnNavigatedTo(NavigationEventArgs e) 
            { 
                this.navigationhelper.OnNavigatedTo(e); 
            } 
    private void navigationhelper_LoadState(object sender, LoadStateEventArgs e) 
            { 
               // throw new NotImplementedException(); 
                try 
                { 
                    string value = e.NavigationParameter as string; 
                    if(!string.IsNullOrWhiteSpace(value)) 
                    { 
                        lst_album.ItemsSource=q.list_album(value); 
                         //get data from front page
                    } 
                    else 
                    { 
                       // txtnotice.Text = "Error: Haven't data on listbox,Please."; 
                    } 
                } 
                catch (Exception ex) 
                { 
                     //dialog = new MessageDialog(ex.Message); 
                    //await dialog.ShowAsync(); 
                   // txtnotice.Text = ex.Message; 
                } 
                 
            } 



    • Edited by Le Thien Hoang Saturday, January 10, 2015 2:53 PM
    • Marked as answer by icce cage Saturday, January 10, 2015 5:14 PM
    Saturday, January 10, 2015 2:51 PM

All replies

  • what error you have? how are the items lost in the listbox?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by icce cage Saturday, January 10, 2015 5:21 PM
    Friday, January 9, 2015 12:08 PM
  • Hello icce cage,

    Can you tell us which error do you get? I tried your code myself and it worked fine.
    Maybe the Problem is that the ListBox "display1" doesn't have so many ListBoxItem as "display" so that the SelectedIndex can not be set with the parameter.
    You can first check if the SelectedIndex can be set with "y" and then set it.

    Regards,
    Bo Liu
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    • Marked as answer by icce cage Saturday, January 10, 2015 5:14 PM
    Friday, January 9, 2015 12:18 PM
    • Marked as answer by icce cage Saturday, January 10, 2015 5:20 PM
    Friday, January 9, 2015 10:54 PM
  • You are developing on RT. You should download my app(https://code.msdn.microsoft.com/Music-online-and-18b2e6d2/sourcecode?fileId=116978&pathId=1927464242) and find  the files name is " common" . Inside of files have 

    -NavigationHelper.cs

    -ObservableDictionary.cs

    -RelayCommand.cs

    -SuspensionManager.cs

    and you add folder to your app.

    the first pages:

    add this code and Note :

    xmlns:common="using:App3.Common" 
    <Page 
        x:Class="App3.BlankPage1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="using:App3" 
        xmlns:common="using:App3.Common" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" Background="White" 
        > 

    and inside of ".cs"

    using App3.Common;

    and

       private void lst_singer_SelectionChanged(object sender, SelectionChangedEventArgs e) 
            { 
                
                if(lst_us.SelectedIndex!=-1) 
                { 
                    //background 
     
                                
                     
                        try 
                        { 
                             
                            string value = (lst_us.SelectedItem as Data.Data).name; 
                            this.Frame.Navigate(typeof(BlankPage1), value); 
                        } 
                        catch (Exception ex) 
                        { 
                            //txtnotice.Text = ex.Message; 
                        } 
                } 
            } 

    The second pages:

    Next steps.

     private NavigationHelper navigationhelper; 
     private ObservableDictionary deafaultviewmodel = new ObservableDictionary(); 

     public BlankPage1() 
            { 
                this.InitializeComponent(); 
                this.navigationhelper = new NavigationHelper(this); 
                this.navigationhelper.LoadState += navigationhelper_LoadState; 
                this.navigationhelper.SaveState += navigationhelper_SaveState; 
                //pivot 
               
                 
            } 

     public NavigationHelper NavigationHelper 
            { 
                get { return this.navigationhelper; } 
            } 
     
            public ObservableDictionary DefaultViewModel 
            { 
                get { return this.deafaultviewmodel; } 
            } 
            protected override void OnNavigatedFrom(NavigationEventArgs e) 
            { 
                this.navigationhelper.OnNavigatedFrom(e); 
            } 
     
            /// <summary> 
            /// Invoked when this page is about to be displayed in a Frame. 
            /// </summary> 
            /// <param name="e">Event data that describes how this page was reached. 
            /// This parameter is typically used to configure the page.</param> 
            protected override void OnNavigatedTo(NavigationEventArgs e) 
            { 
                this.navigationhelper.OnNavigatedTo(e); 
            } 
    private void navigationhelper_LoadState(object sender, LoadStateEventArgs e) 
            { 
               // throw new NotImplementedException(); 
                try 
                { 
                    string value = e.NavigationParameter as string; 
                    if(!string.IsNullOrWhiteSpace(value)) 
                    { 
                        lst_album.ItemsSource=q.list_album(value); 
                         //get data from front page
                    } 
                    else 
                    { 
                       // txtnotice.Text = "Error: Haven't data on listbox,Please."; 
                    } 
                } 
                catch (Exception ex) 
                { 
                     //dialog = new MessageDialog(ex.Message); 
                    //await dialog.ShowAsync(); 
                   // txtnotice.Text = ex.Message; 
                } 
                 
            } 



    • Edited by Le Thien Hoang Saturday, January 10, 2015 2:53 PM
    • Marked as answer by icce cage Saturday, January 10, 2015 5:14 PM
    Saturday, January 10, 2015 2:51 PM
  • Thanks i already solve it here's my code.. i created a textblock on both mainpage and secondPage and a parameter...

    MainPage.cs:

     private void display_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {          
                 Frame.Navigate(typeof(FullView), display.SelectedIndex);
            }

    My SecondPage;

    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                string w = tex.Text = e.Parameter.ToString();
                int t = Convert.ToInt32(w);
    
                tex.Text = e.Parameter.ToString();
                display1.SelectedIndex = t;
            }

    Saturday, January 10, 2015 5:18 PM