[英]bind multiple files to wpf datagrid
Im new to WPF
and i'm trying to bind songs from a directory to a datagrid using MVVM
. 我是WPF
新手,我正在尝试使用MVVM
将歌曲从目录绑定到datagrid。 So far i have been able to bind one song file using a "foreach loop" but i'm unable to come up with the logic that will display the rest of the songs. 到目前为止,我已经能够使用“ foreach循环”绑定一个歌曲文件,但是我无法提出将显示其余歌曲的逻辑。 Here is what i have done so far: 到目前为止,这是我所做的:
using System.IO;
namespace MusicPlayer
{
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Songs = GetSongs();
}
public Songs Songs { get; set; }
string title;
string artist;
string album;
uint year;
string genre;
public Songs GetSongs()
{
DirectoryInfo di = new DirectoryInfo("C:/Users/USER/MyMusic");
FileInfo[] Files = di.GetFiles("*.mp3"); //Getting mp3 files
foreach (FileInfo file in Files)
{
string fileName = file.FullName;
TagLib.File tagFile = TagLib.File.Create(fileName);
title = tagFile.Tag.Title;
artist = tagFile.Tag.FirstAlbumArtist;
album = tagFile.Tag.Album;
year = tagFile.Tag.Year;
genre = tagFile.Tag.FirstGenre;
//string duration = tagFile.Tag.time;
}
return new Songs { new Song { Name = title, Artist = artist, Album = album, Year = year, Genre = genre } };
}
}
}
the Songs Class 歌曲课
using System.Collections.ObjectModel;
namespace MusicPlayer
{
public class Songs : ObservableCollection<Song>
{
}
}
the Song Class 歌曲课
namespace MusicPlayer
{
public class Song
{
public string Name { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public string Duration { get; set; }
public uint Year { get; set; }
public string Genre { get; set; }
}
}
the xaml.cs file xaml.cs文件
using System.Windows;
namespace MusicPlayer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
MainWindowViewModel mainWindowViewModel;
public MainWindow()
{
InitializeComponent();
mainWindowViewModel = new MainWindowViewModel();
DataContext = mainWindowViewModel;
}
}
}
As it looks from your code-snippet, you only add one song to the list. 从您的代码片段看,您只将一首歌曲添加到列表中。 As well if the Songs class really just derives from ObservableCollection you can remove it and directly use ObservableCollection. 同样,如果Songs类确实只是从ObservableCollection派生的,则可以将其删除并直接使用ObservableCollection。 So initialize the list before your foreach loop and then add songs as part of the loop: 因此,请在foreach循环之前初始化列表,然后在循环中添加歌曲:
DirectoryInfo di = new DirectoryInfo("C:/Users/USER/MyMusic");
FileInfo[] Files = di.GetFiles("*.mp3"); //Getting mp3 files
var songs = new ObservableCollection<Song>();
foreach (FileInfo file in Files)
{
string fileName = file.FullName;
TagLib.File tagFile = TagLib.File.Create(fileName);
title = tagFile.Tag.Title;
artist = tagFile.Tag.FirstAlbumArtist;
album = tagFile.Tag.Album;
year = tagFile.Tag.Year;
genre = tagFile.Tag.FirstGenre;
//string duration = tagFile.Tag.time;
songs.Add(new Song { Name = title, Artist = artist, Album = album, Year = year, Genre = genre });
}
return songs;
Your Songs Property then would be of type ObservableCollection instead. 您的Songs属性将改为ObservableCollection类型。 Furthermore you can skip the public setter and just keep the getter to make it a readonly property (as we only set it in the constructor that's fine): 此外,您可以跳过公共设置器,而只保留getter使其成为只读属性(因为我们只在正常的构造函数中设置了它):
public MainWindowViewModel()
{
Songs = GetSongs();
}
public ObservableCollection<Song> Songs { get; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.