Hi am using XAML file given below.I want to Navigate Listbox selected item to another page.
In the SelectionChanged event handler of ListBox,, add this line
override NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = NotchsList11.Items[NotchsList11.SelectedIndex] as YourClassUsedForBinding;
if(item.SomeProperty == "Something")
NavigationService.Navigate(new Uri("/YourNewPage.xaml", UriKind.Relative));
else
NavigationService.Navigate(new Uri("/YourOtherPage.xaml", UriKind.Relative));
//if more cases use a switch case
}
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
<StackPanel Width="Auto">
<StackPanel VerticalAlignment="Top" Width="Auto">
<ListBox ItemsSource="{Binding Articles}" Margin="5,5,5,0" Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionChanged="NotchsList11_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="#302c2d" Background="#302c2d" BorderThickness="5,5,5,0" Name="image" Margin="3,0,3,0">
<Image Source="{Binding Image}" Width="141" Height="95" Name="value" Stretch="Fill" VerticalAlignment="Top"></Image>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Width="Auto">
<ListBox x:Name="NotchsList11" ItemsSource="{Binding Articles}" Margin="5,0,5,0" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionChanged="NotchsList11_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderBrush="#302c2d" Background="#302c2d" BorderThickness="5,5,5,0" Margin="3,0,3,0">
<TextBlock Text="{Binding Titles}" Width="141" Height="90" Padding="3,0,0,30" TextWrapping="Wrap"></TextBlock>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>
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");
}
You have set ItemsSource="{Binding Images}"
for your list box which means that you should not cast to Notch
in your SelectionChangedHandler
. Every item receives an image as its data context which is an element from the list Images
inside a Notch
instance.
Therefore, you can only cast to String
inside your handler.
You have a top-level listbox and each item again has its own listbox. You are tracking the selection changes for the inner list and in that case your data context is individual item for that list. You do not have access to parent scope inside it.