首页 > 解决方案 > 使用自定义类中的列表填充 DataGrid

问题描述

我有一个具有包含自定义对象列表的属性的类。

// MySongs.cs
public class MySongs
{    
    public List<Song> Songs = new List<Song>();
}

Songs 属性填充在MainWindow()中。

// MainWindow.xaml.cs
MySongs.Songs.Add(new Song("Hey Jude", "The Beatles"));

如何在标题和艺术家作为标题的 DataGrid 中显示MySongs.Songs列表?

谢谢!

编辑(10/27):

这是MainWindow.xaml中的 XAML :

<Window x:Class="MySongsUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MySongsUI"
    mc:Ignorable="d"
    Title="My Songs" Height="450" Width="800">

    <Grid>

        <!-- DataGrid with Songs -->
        <DataGrid x:Name="songsDataGrid" />

    </Grid>

</Window>

这是MySongs.cs中的 C# :

using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;

namespace MySongsUI
{
    public static class MySongs
    {
        public static ObservableCollection<Song> Songs = new ObservableCollection<Song>();
    }
}

这是Song.cs中的 C# :

namespace MySongsUI
{
    public class Song
    {
        public string Title { get; set; }
        public string Artist { get; set; }

        // CONSTRUCTOR
        public Song(string title, string artist)
        {
            Title = title;
            Artist = artist;
        }
    }
}

我想我不确定在MainWindow.xaml的 XAML 中识别MySongs类的最佳方法,因此我可以将它绑定到 DataGrid。

谢谢!

标签: c#wpfdatagrid

解决方案


这是一个非常简单的示例代码,它将根据 Song 类的属性自动生成所有列:

<DataGrid ItemsSource="{Binding MySongs}" AutoGenerateColumns="True">
</DataGrid >

如果您想要任何自定义,则需要对其进行样式设置。

如果您想在运行时对它们进行更改,请不要忘记将列表设置为 ObservableCollection 和从 NotificationObject 继承的 Song 类。

希望这可以帮助..

编辑:

这是它的样子:

主窗口:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <local:MainView />
</Window>

MainView.xaml.cs:

public MainView()
        {
            DataContext = new MainViewModel();
            InitializeComponent();
        }

MainView.xaml:

<DataGrid ItemsSource="{Binding local:MySongs.Songs}" AutoGenerateColumns="True">
    </DataGrid >

MainViewModel.cs:

public class MainViewModel
    {
        public MySongs MySongs { get; set; }

        public MainViewModel()
        {
            MySongs = new MySongs()
            {
                Songs = new ObservableCollection<Song>()
                {
                    new Song("Hey Jude", "The Beatles")
                }
            };
        }
    }

您必须在 MainViewModel、Song 和 MySongs 上实现 INotifyPropertyChanged 以支持数据的运行时更改。


推荐阅读