向列表框中添加新媒体项

本文关键字:新媒体 添加 列表 | 更新日期: 2023-09-27 18:16:54

我有2个类:Multimedia和MultimediaList和2个窗口:我的主窗口和输入窗口,我在那里插入我的数据,以便向列表框添加新项。

我的多媒体类是这样的:

public class Multimedia : INotifyPropertyChanged
{
    public enum MediaType
    {
        CD,
        DVD
    };
    private string _title;
    private string _artist;
    private string _genre;
    private MediaType _type;
    public Multimedia(string title, string artist, string genre, MediaType type)
    {
        _title = title;
        _artist = artist;
        _genre = genre;
        _type = type;
    }
    public String Title
    {
        get { return this._title; }
        set { this._title = value; }
    }
    public String Artist
    {
        get { return this._artist; }
        set { this._artist = value; }
    }
    public String Genre
    {
        get { return this._genre; }
        set { this._genre = value; }
    }
    public MediaType Type
    {
        get { return this._type; }
        set { this._type = value; }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
这是我的MultimediaList类:
 public class MultimediaList : ObservableCollection<Multimedia>
{
    public MultimediaList()
    {
        Add(new Multimedia("Blaster", "UFK", "Dubstep", Multimedia.MediaType.CD));
        Add(new Multimedia("Hello", "Habstrakt", "Dubstep", Multimedia.MediaType.DVD));
        Add(new Multimedia("Black Veil", "Straight Line Stich", "Metal", Multimedia.MediaType.CD));
        Add(new Multimedia("Ridiculous", "Dope D.O.D", "Hip-Hop", Multimedia.MediaType.DVD));
    }
    public void addItem(string title, string artist, string genre, Multimedia.MediaType type)
    {
        Add(new Multimedia(title, artist, genre, type));                      
    }
}

列表框中填充了在程序中手动设置的项目,因此可以工作,但现在我想使用第二个窗口将项目添加到列表框中。

这是我的第二个窗口设置:

private MultimediaList myList = new MultimediaList();
    public InputWindow()
    {
        InitializeComponent();
        PopulateComboBox();
    }
    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        myList.addItem(textBox.Text, textBox1.Text, textBox2.Text, Multimedia.MediaType.CD);
        //Close();
    }
XML:

<ListBox DisplayMemberPath="Title" Name="listBox1" ...></ListBox>

每当我按下save按钮时,主窗口中的列表框都不会被新数据填充。有人能帮我一下吗?

这是我的主窗口代码:
public MainWindow()
    {
        InitializeComponent();
        myList = new MultimediaList();
        listBox1.ItemsSource = myList;
    }
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        ModalWindow input = new ModalWindow();
        input.ShowDialog();
    }

向列表框中添加新媒体项

必须使用两个表单的实例在两个表单之间传递数据。请参阅下面的示例代码

形式1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}
​

形式2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();
            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }
    }
}
​