【问题标题】:bind multiple files to wpf datagrid将多个文件绑定到 wpf 数据网格
【发布时间】:2018-02-19 17:33:56
【问题描述】:

我是WPF 的新手,我正在尝试使用MVVM 将目录中的歌曲绑定到数据网格。到目前为止,我已经能够使用“foreach 循环”绑定一首歌曲文件,但我无法想出显示其余歌曲的逻辑。这是我到目前为止所做的:

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 } };
    }
}
}

歌曲课

using System.Collections.ObjectModel;

namespace MusicPlayer
{
    public class Songs : ObservableCollection<Song>
    {
    }
}

歌曲班

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; }
    }
}

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;          
        }

    }
}

【问题讨论】:

  • 请添加Songs
  • @Fruchtzwerg 抱歉,我已经用其余代码更新了帖子

标签: c# wpf mvvm


【解决方案1】:

从您的代码-sn-p 中可以看出,您只需将一首歌曲添加到列表中。同样,如果 Songs 类真的只是派生自 ObservableCollection,您可以将其删除并直接使用 ObservableCollection。 所以在你的 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;

您的 Songs 属性将改为 ObservableCollection 类型。此外,您可以跳过公共 setter,只保留 getter 以使其成为只读属性(因为我们只在构造函数中设置它就可以了):

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

public ObservableCollection<Song> Songs { get; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2011-01-31
    • 2017-05-14
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多