2014-04-27 72 views
-1

我使用的ObservableCollection綁定到列表框中的一些字符串: 這裏是我的SongFinder代碼:綁定列表框只能查看最後一項

 string title, downloadlink, artist, duration; 
    private readonly HtmlWeb web = new HtmlWeb(); 
    public string Title 
    { 
     get { return title; } 
     set { title = value; } 
    } 
    public string DownloadLink 
    { 
     get { return downloadlink; } 
     set { downloadlink = value; } 
    } 
    public string Artist 
    { 
     get { return artist; } 
     set { artist = value; } 
    } 
    public string Duration 
    { 
     get { return duration; } 
     set { duration = value; } 
    } 

    public SongFinder(string pageuri) 
    { 

    } 
    public void Mp3Monkey(string pageUri) 
    { 
     HtmlDocument doc = web.Load(pageUri); 
     HtmlNode documentNode = doc.DocumentNode; 
     doc.OptionUseIdAttribute = true; 
     var xpath = "//div[@class='dd']/noindex/div"; 
     HtmlNodeCollection col = documentNode.SelectNodes(xpath); 


     foreach (var n in col) 
     { 
      downloadlink = "https://www.mp3monkey.net/audio/" + n.SelectSingleNode("span[@id='oid']").InnerText + "/" + n.SelectSingleNode("span[@id='aid']").InnerText + "/" + 
         n.SelectSingleNode("span[@id='autor']").InnerText + "_-_" + n.SelectSingleNode("span[@id='title']").InnerText.Replace(" ", "_") + ".mp3"; 

      artist = n.SelectSingleNode("span[@id='autor']").InnerText; 
      title = n.SelectSingleNode("span[@id='title']").InnerText; 
      TimeSpan t = TimeSpan.FromSeconds(double.Parse(n.SelectSingleNode("span[@id='time']").InnerText)); 
      string answer = string.Format("{0:D2}:{1:D2}:{2:D2}", 
      t.Hours, 
      t.Minutes, 
      t.Seconds); 
      duration = answer; 
     } 
    } 

這裏是我的代碼來觀察集合綁定到listbox:

<ListBox x:Name="list" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="365" Margin="73,187,0,0" VerticalAlignment="Top" Width="460"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Title}"/> 
        <TextBlock Text="{Binding Artist}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

這是我的Code-Behind;

 this.DataContext = net; 

     string Mp3Monkey1 = "Eminem Mp3 Download.htm"; 
     SongFinder find = new SongFinder(@"D:\books\" + Mp3Monkey1); 
     find.Mp3Monkey(@"D:\books\" + Mp3Monkey1); 
     net.Add(find); 

現在我遇到的問題是,當所有的信息加載時,列表框只顯示集合中的最後一首歌曲。請幫忙!! 如果我嘗試調試沒有綁定它工作正常,並加載每首歌的所有信息。

爲什麼我想知道?

任何幫助,將不勝感激

+0

您正在覆蓋您的循環中的屬性。 – Aybe

+0

@Aybe哦!但你知道如何解決這個問題? – TheGaMeR123

+0

看到我的答案。 – Aybe

回答

1

在你的代碼你覆蓋你的財產(藝術家,持續時間,等...),所以很明顯,你將只能看到最後一首歌的foreach循環處理。

解決方案:

使用一個集合,您將在環在你的HTMLNodeCollection

實例創建每個節點的新Song填寫:

代碼

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Loaded += MainWindow_Loaded; 
     } 

     private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      // Let's pretend this is your HtmlNodeCollection 
      var collection = new List<Song> 
      { 
       new Song 
       { 
        Artist = "Joeski & Chus", 
        Title = "El Amor", 
        Duration = TimeSpan.FromMinutes(5).TotalSeconds 
       }, 
       new Song 
       { 
        Artist = "Dano & Joeski", 
        Title = "For your love", 
        Duration = TimeSpan.FromMinutes(10).TotalSeconds 
       }, 
      }; 

      // Build objects from your HTML nodes ... 
      var songs = new ObservableCollection<Song>(); 
      foreach (Song song in collection) 
      { 
       songs.Add(song); 
      } 

      DataContext = songs; 
     } 
    } 

    internal class Song 
    { 
     public string Artist { get; set; } 
     public string Title { get; set; } 
     public double Duration { get; set; } 
    } 
} 

XAML

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
     Title="MainWindow" 
     Width="525" 
     Height="350"> 
    <Grid> 
     <ListBox ItemsSource="{Binding}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate DataType="wpfApplication1:Song"> 
        <Border Margin="2" 
          BorderBrush="Red" 
          BorderThickness="1"> 
         <StackPanel> 
          <TextBlock Text="{Binding Artist, StringFormat='{}Artist: {0}'}" /> 
          <TextBlock Text="{Binding Title, StringFormat='{}Title: {0}'}" /> 
          <TextBlock Text="{Binding Duration, StringFormat='{}Duration: {0}'}" /> 
         </StackPanel> 
        </Border> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Window> 

結果

enter image description here

編輯

與示例代碼:

var songs = new ObservableCollection<Song>(); 
foreach (var n in col) 
{ 
    Song song = new Song(); 
    downloadlink = "https://www.mp3monkey.net/audio/" + n.SelectSingleNode("span[@id='oid']").InnerText + "/" + n.SelectSingleNode("span[@id='aid']").InnerText + "/" + 
       n.SelectSingleNode("span[@id='autor']").InnerText + "_-_" + n.SelectSingleNode("span[@id='title']").InnerText.Replace(" ", "_") + ".mp3"; 

    song.Artist = n.SelectSingleNode("span[@id='autor']").InnerText; 
    song.Title = n.SelectSingleNode("span[@id='title']").InnerText; 
    // etc ... 

    songs.Add(song);  
} 

DataContext = songs; 

要確保你使用我的Song類和ListBox以及我在XAML中定義的DataTemplate

+0

我還是不明白。我如何用HtmlNodeCollection替換集合?你的例子din't幫了我很多....請嘗試解釋更多。 – TheGaMeR123

+0

怎麼回事?我認爲這很容易理解,您需要一組對象,以便從HTML節點集合中的每個節點創建對象集合。以前你覆蓋你的屬性,並沒有收集,所以這就是爲什麼你只看到一個對象。請參閱我的編輯,瞭解您的foreach循環的示例。 – Aybe