How to set Page Navigation from Listbox to another Page?

后端 未结 3 1855
情话喂你
情话喂你 2020-12-21 14:20

Hi am using XAML file given below.I want to Navigate Listbox selected item to another page.

 

        
3条回答
  •  -上瘾入骨i
    2020-12-21 14:28

       private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
    
            var lb = sender as ListBox;
            if (lb == null) return;
            var articleSubItem = lb.SelectedItem as NotchSubItem;
            if (articleSubItem == null)  return;
    
            App.CurrentArticle = articleSubItem;
            NavigationService.Navigate(new Uri("/Test.xaml?selectedItem=" + articleSubItem.ArticleId, UriKind.Relative));
            NotchsList11.SelectedIndex = -1;
        }
    

    To set details page

       protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                title.Text = App.CurrentArticle.Titles;
    
                webBrowser.NavigateToString(App.CurrentArticle.FullContent);
    
            }
            base.OnNavigatedTo(e);
    
        }
    

    MainPage.xaml page

                  
                                
                                    
                                        
                                            
                                                
                                                
                                            
                                        
                                        
                                            
                                                
                                                    
                                                
                                            
                                        
                                    
                                
                                
                                    
                                        
                                            
                                                
                                                
                                            
                                        
                                        
                                            
                                                
                                                    
                                                
                                            
                                        
                                    
                                
                            
    

    MainPage.xaml.cs

    XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
    
                var query = from l in xmlDoc.Descendants("Category")
                            select new Notch
                            {
                                name = (string)l.Attribute("name").Value,
                                Titles = l.Element("Articles").Elements("article")
                                         .Select(s => s.Attribute("title").Value)
                                         .ToList(),
    
    
                                Articles = l.Element("Articles").Elements("article")
                                            .Select(article =>  new NotchSubItem
                                            {
                                            Image = article.Element("thumb_image").Element("image").Attribute("url").Value,
                                            ArticleId = article.Attribute("articleid").Value,
                                            FullContent = article.Element("FullContent").Value.ToString(),
                                            Titles = article.Attribute("title").Value,
                                            })
                                            .ToList(),
    
                                Images = l.Element("Articles").Elements("article").Elements("thumb_image").Elements("image")
                                         .Select(x => x.Attribute("url").Value).ToList(),
    
                            };
    
                            foreach (var result in query)
                            {
                                Console.WriteLine(result.name);
                                foreach (var detail in result.Titles.Zip(result.Images, (st, si) => string.Format("{0} {1}", st, si)))
                                {
                                    Console.WriteLine(detail);
    
                                }
    
                            }
                            NotchsList11.ItemsSource = query;
            }
            catch(Exception e)
            {
                MessageBox.Show("Binding Failed");
            }
    

提交回复
热议问题