【问题标题】:Populating a DataGrid with a List from a custom Class使用自定义类中的列表填充 DataGrid
【发布时间】:2019-03-31 19:13:19
【问题描述】:

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

// 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# wpf datagrid


【解决方案1】:

这是一个非常简单的示例代码,它将根据 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 以支持数据的运行时更改。

【讨论】:

  • 我需要在某处或某处设置 DataContext 吗?我没有将任何数据输入 DataGrid,而 MySongs 中有数据。
  • 如果我在代码中设置 ItemSource,它会按预期工作,但我希望能够在 XAML 中设置 ItemSource。 songDataGrid.ItemsSource = MySongs.Songs;
  • 你能添加你的 xaml 吗?
  • @T Gregory 我已经编辑了我的答案并添加了一些示例代码来解释我的答案。
  • 你做错了我的朋友,阅读 MVVM Methodology 以更好地正确理解 wpf 应用程序的开发。同时,看看我的回答,它是正确的 MVVM,将帮助您了解它。
猜你喜欢
  • 2011-05-03
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多