C# 仅查看最后一项的绑定列表框
我正在使用ObservableCollection将一些字符串绑定到列表框: 以下是我的SongFinder代码:C# 仅查看最后一项的绑定列表框,c#,wpf,html-agility-pack,observablecollection,code-behind,C#,Wpf,Html Agility Pack,Observablecollection,Code Behind,我正在使用ObservableCollection将一些字符串绑定到列表框: 以下是我的SongFinder代码: string title, downloadlink, artist, duration; private readonly HtmlWeb web = new HtmlWeb(); public string Title { get { return title; } set { title = value; }
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 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>
现在我遇到的问题是,当加载所有信息时,列表框只显示集合中的最后一首歌曲。请帮忙!!
如果我尝试在不绑定的情况下进行调试,它可以正常工作,并加载每首歌曲的所有信息
为什么我想知道
如果您的代码覆盖了您的属性(艺术家、持续时间等),那么您将非常感谢您的帮助,因此显然您将只看到
foreach
循环处理的最后一首歌曲
解决方案:
通过从HTMLNodeCollection
示例:
代码
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Windows;
命名空间WpfApplication1
{
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
加载+=主窗口\u加载;
}
已加载私有void主窗口(对象发送器、路由目标)
{
//让我们假设这是你的HtmlNodeCollection
var集合=新列表
{
新歌
{
艺人=“Joeski&Chus”,
Title=“El Amor”,
持续时间=时间跨度。从分钟(5)到总秒
},
新歌
{
艺术家=“达诺和乔斯基”,
Title=“为了你的爱”,
持续时间=时间跨度。从分钟(10)。总秒
},
};
//从HTML节点生成对象。。。
var songs=新的ObservableCollection();
foreach(收藏歌曲)
{
歌曲。添加(歌曲);
}
数据上下文=歌曲;
}
}
内部类歌曲
{
公共字符串艺术家{get;set;}
公共字符串标题{get;set;}
公共双持续时间{get;set;}
}
}
XAML
结果
编辑
代码示例:
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;
var songs=新的ObservableCollection();
foreach(列中的变量n)
{
歌曲=新歌();
下载链接=”https://www.mp3monkey.net/audio/“+n.SelectSingleNode(“span[@id='oid']””)。InnerText+”/“+n.SelectSingleNode(“span[@id='aid']”)。InnerText+”/”+
n、 SelectSingleNode(“span[@id='autor']”)。InnerText+“\u-\ u”+n.SelectSingleNode(“span[@id='title']”)。InnerText.Replace(“,”)+”.mp3”;
song.Artist=n.SelectSingleNode(“span[@id='autor']);
song.Title=n.SelectSingleNode(“span[@id='Title']);
//等等。。。
歌曲。添加(歌曲);
}
数据上下文=歌曲;
确保您确实使用了我在XAML中定义的
歌曲
类和列表框
以及数据模板
。您正在覆盖循环中的属性。@Aybe-Oh!!!但你知道怎么解决吗?我还是不明白。如何用HtmlNodeCollection替换集合??你的例子对我帮助不大。。。。请再解释一下,怎么会呢?我认为这很容易理解,您需要一个对象集合,因此您可以从HTML节点集合中的每个节点创建一个对象集合。以前,您正在覆盖属性,并且没有集合,因此您只能看到一个对象。有关foreach循环的示例,请参见我的编辑。
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; }
}
}
<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>
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;