我使用的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);
現在我遇到的問題是,當所有的信息加載時,列表框只顯示集合中的最後一首歌曲。請幫忙!! 如果我嘗試調試沒有綁定它工作正常,並加載每首歌的所有信息。
爲什麼我想知道?
任何幫助,將不勝感激
您正在覆蓋您的循環中的屬性。 – Aybe
@Aybe哦!但你知道如何解決這個問題? – TheGaMeR123
看到我的答案。 – Aybe