Winforms c#listbox不显示列表的内容

时间:2015-08-31 18:31:51

标签: c# winforms

我有一个winforms应用程序,我正在尝试使用MVC编程

在我的表单的designer.cs中:

    this.listBox_regApps = new System.Windows.Forms.ListBox();
    this.listBox_regApps.Anchor = System.Windows.Forms.AnchorStyles.Top;
    this.listBox_regApps.FormattingEnabled = true;
    this.listBox_regApps.Location = new System.Drawing.Point(62, 115);
    this.listBox_regApps.Name = "listBox_regApps";
    this.listBox_regApps.Size = new System.Drawing.Size(351, 134);
    this.listBox_regApps.TabIndex = 1;

然后在普通的cs文件中,我尝试将列表框数据设置为列表(apps)。当我调试列表应用确实有数据,但它永远不会出现在表单中。不知道为什么。我尝试过添加.update和.refresh,但这些都没有。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppCompatDemo1
{
    interface IAppView
    {
        void AddListener(IController controller);
    };



    public partial class AppCompatApp : Form, IAppView
    {
        IController controller;
        public AppCompatApp()
        {

            InitializeComponent();
        }

        public void AddListener(IController controller)
        {
            this.controller = controller;
        }

        //Trying to set my listbox to display this list
        public void SetApps(List<string> apps)
        {
            listBox_regApps.DataSource = apps;
        }




    }

    public interface IController
    {
        void OnTestClick(string currentApp);
    }

    class AppController : IController
    {
        AppCompatApp view;
        IAppModel model;

        public AppController(IAppModel model, AppCompatApp view)
        {
            this.model = model;
            this.view = view;
            this.view.AddListener(this);
            view.SetApps(model.getApps());
        }

    }

    interface IAppModel
    {
        List<string> getApps();
    }

    class AppModel : IAppModel
    {
        List<string> apps;
        public AppModel()
        {
            apps = new List<string>();
            apps.Add("app1");
            apps.Add("app2");
            apps.Add("app3");
        }

        public List<string> getApps()
        {
            return apps;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

像这样。

foreach (string app in apps)
{
    listBox_regApps.Items.Add(app);
}