简体   繁体   中英

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 . 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. 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

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. So initialize the list before your foreach loop and then add songs as part of the loop:

    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. 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):

public MainWindowViewModel()
{
    Songs = GetSongs();
}

public ObservableCollection<Song> Songs { get; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM