将新媒体项添加到列表框

时间:2015-10-09 17:29:30

标签: c# xaml

我有2个类:Multimedia和MultimediaList以及2个窗口:我的主窗口和输入窗口,我在其中插入数据以便将新项目添加到列表框中。

我的Multimedia课程如下所示:

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>

每当我按下保存按钮时,我的主窗口中的列表框都没有填充新数据。任何人都可以帮我这个吗?

这是我的主要窗口代码:

public MainWindow()
    {
        InitializeComponent();
        myList = new MultimediaList();
        listBox1.ItemsSource = myList;
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        ModalWindow input = new ModalWindow();
        input.ShowDialog();
    }

1 个答案:

答案 0 :(得分:1)

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

表格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";
        }

    }
}
​