Winforms c#列表框不显示列表内容
本文关键字:列表 显示 Winforms | 更新日期: 2023-09-27 18:10:45
我有一个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文件中,我尝试将列表框数据设置为列表(应用程序)。当我调试列表时,应用程序确实有数据,但它从未出现在表单中。不知道为什么。我试过添加。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;
}
}
}
就像这样。
foreach (string app in apps)
{
listBox_regApps.Items.Add(app);
}