简体   繁体   English

如何在C#中创建列表集合并为其初始化数据

[英]How can I Create a Collection of Lists in C# and initialize data to it

I want to create a sample data for media on a windows store application, I created a class DigitalMedia shown below 我想为Windows商店应用程序上的媒体创建示例数据,我创建了如下所示的DigitalMedia类

public class DigitalMedia
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string Subject { get; set; }
    public string Format { get; set; }
    public double Duration { get; set; }
    public string Description { get; set; }
    public ImageSource TitleImage { get; set; }
    public ImageSource Thumbnail { get; set; }
    public Uri PurchaseLink { get; set; }
}

I also created another class called GroupedMedia to represent a group of the DigitalMedia class. 我还创建了另一个名为GroupedMedia的类来表示DigitalMedia类的组。 this code is below 此代码如下

public class GroupedMedia : INotifyPropertyChanged
{
    public string GroupTitle { get; set; }
    public string Description { get; set; }
    public ImageSource GroupImage { get; set; }

    private ObservableCollection<DigitalMedia> _mediaList = null;
    public ObservableCollection<DigitalMedia> MediaList 
    {
        get 
        {
            if (_mediaList == null)
                _mediaList = new ObservableCollection<DigitalMedia>();
                return _mediaList;
        }
        set 
        {
            _mediaList = value; 
            RaisePropertyChanged("MediaList");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
  • How do I add sample data to the DigitalMedia collection and also add data to the GroupedMedia collection ? 如何将样本数据添加到DigitalMedia集合中,以及如何将数据添加到GroupedMedia集合中?
  • How do I set the DefaultViewModel of the MainPage.xaml.cs to this collection ? 如何将MainPage.xaml.cs的DefaultViewModel设置为此集合?
  • How do I bind the collection containing the data to a CollectionViewSource so that I can use it in a GridView in XAML page ? 如何将包含数据的集合绑定到CollectionViewSource,以便可以在XAML页面的GridView中使用它?

1- If this is just for testing purpose, you could simply add your initialization of your objects below the InitializeComponent() of your MainWindow.xaml.cs. 1-如果只是出于测试目的,则可以简单地将对象的初始化添加到MainWindow.xaml.cs的InitializeComponent()下。

3- Put a DataGrid in your mainwindow like this: 3-像这样在主窗口中放置一个DataGrid:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="dgTest"></DataGrid>
    </Grid>
</Window>

And then set its ItemsSource to your data. 然后将其ItemsSource设置为您的数据。

public MainWindow()
        {
            InitializeComponent();

            //Initialize your data here
            GroupedMedia gm = new GroupedMedia();
            //Initialize your data here

            this.dgTest.ItemsSource = gm.MediaList;
        }

Try something like this 试试这个

       static void Main(string[] args)
        {

            GroupedMedia groupedMedia = new GroupedMedia();
            List<DigitalMedia> digitalMedias = new List<DigitalMedia>();
            for(int i = 0; i < 5; i++)
            {
               DigitalMedia digitalMedia = new DigitalMedia();
                digitalMedias.Add(digitalMedia);
                digitalMedia.Author = "John";
            }
            groupedMedia.MediaList = digitalMedias;


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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