简体   繁体   中英

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

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. 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 ?
  • How do I set the DefaultViewModel of the MainPage.xaml.cs to this collection ?
  • How do I bind the collection containing the data to a CollectionViewSource so that I can use it in a GridView in XAML page ?

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.

3- Put a DataGrid in your mainwindow like this:

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

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;


        }

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