locked
ListBox on selection changed RRS feed

  • Question

  • Hi,

    Code of my listbox

    <ListBox Name="feedListBox" Height="468" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="444" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel VerticalAlignment="Top">
                                <TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Path=Title}" />
                                <TextBlock  FontSize="18" Name="artist" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{ Binding  Path=Artist}" />
                                <TextBlock FontSize="16" Name="lastweek" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Path=last_week}" />
                                <TextBlock FontSize="16" Name="thisweek" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Path=this_week}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    and listbox items source

    var rssData = from rss in XElement.Parse(feedXML).Descendants("item") select new RSSClass { Title = rss.Element("title").Value, Artist = rss.Element("artist").Value, last_week="lastweek"+rss.Element("rank_last_week").Value, this_week="thisweek"+rss.Element("rank_this_week").Value, }; feedListBox.ItemsSource = rssData

    I want to display the textblock's text(artist textblock) of selected listboxitem... on selection changed event occurence.

    eg. if i selected 4th item in listbox.. i want to retrieve 4th items artist textblock text value..

    how can i achieve this??

    thank you


    pradeep lanka

    Thursday, October 10, 2013 12:08 PM

Answers

  • Hello pradeeplanka,

    try to your all data to one collection list, than bind to listbox after binding that code works :

    //selection changed event
    private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    class2 a = (class2)feedListBox.SelectedItem;
    //you got your value
    string s = a.Artist;
    }
    
     var rssData = from rss in XElement.Parse(feedXML).Descendants("item")
                              select new RSSClass
                              {
                                  Title = rss.Element("title").Value,
    
                                  Artist = rss.Element("artist").Value,
                                  last_week="lastweek"+rss.Element("rank_last_week").Value,
                                  this_week="thisweek"+rss.Element("rank_this_week").Value,
                                  
                              };
    ObservableCollection<class2> c2 = new ObservableCollection<class2>();
    foreach(var b in rssData)
    {
    c2.Add(new class2{Artist =b.Artist , last_week =b.last_week ,this_week = b.this_week});
    }
    this.feedListBox.ItemsSource  = c2;
    
    public class class2
    {
    public string Artist {get;set;}
    public string last_week {get;set;}
    public string this_week{get;set;}
    }

    • Marked as answer by pradeeplanka Thursday, October 10, 2013 12:36 PM
    Thursday, October 10, 2013 12:24 PM

All replies

  • Hello pradeeplanka,

    try to your all data to one collection list, than bind to listbox after binding that code works :

    //selection changed event
    private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    class2 a = (class2)feedListBox.SelectedItem;
    //you got your value
    string s = a.Artist;
    }
    
     var rssData = from rss in XElement.Parse(feedXML).Descendants("item")
                              select new RSSClass
                              {
                                  Title = rss.Element("title").Value,
    
                                  Artist = rss.Element("artist").Value,
                                  last_week="lastweek"+rss.Element("rank_last_week").Value,
                                  this_week="thisweek"+rss.Element("rank_this_week").Value,
                                  
                              };
    ObservableCollection<class2> c2 = new ObservableCollection<class2>();
    foreach(var b in rssData)
    {
    c2.Add(new class2{Artist =b.Artist , last_week =b.last_week ,this_week = b.this_week});
    }
    this.feedListBox.ItemsSource  = c2;
    
    public class class2
    {
    public string Artist {get;set;}
    public string last_week {get;set;}
    public string this_week{get;set;}
    }

    • Marked as answer by pradeeplanka Thursday, October 10, 2013 12:36 PM
    Thursday, October 10, 2013 12:24 PM
  • Thank you Khant Nipun ..It works Ive marked it as answered.

    pradeep lanka

    Thursday, October 10, 2013 12:36 PM