Winforms ListViewItems没有显示

时间:2016-07-21 11:48:42

标签: c# winforms listview

为什么在SetListView(ListView listView)中调用Form1 ListViewItems后没有在ListView中显示?

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form, IView
    {
        Presenter presenter;
        public Form1()
        {
            InitializeComponent();
            presenter = new Presenter(this, new Model());
        }

        public void SwitchToControl(UserControl userControl)
        {
            this.tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.Controls.Add(userControl);
        }
        public void SetListView(ListView listView)
        {
            this.listView1 = listView;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            presenter.SwitchToFirst();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            presenter.SwitchToSecond();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            presenter.SetListView();
        }

    }
    interface IView
    {
        void SwitchToControl(UserControl userControl);
        void SetListView(ListView listView);
    }
    class Presenter
    {
        private readonly IView view;
        private readonly IModel model;

        public Presenter(IView view, IModel model)
        {
            this.view = view;
            this.model = model;
        }

        public void SwitchToFirst()
        {
            var control = new UserControl1();
            view.SwitchToControl(control);
        }

        public void SwitchToSecond()
        {
            var control = new UserControl2();
            view.SwitchToControl(control);
        }

        public void SetListView()
        {
            ListView listView = new ListView();
            listView.Items.Add(new ListViewItem { Text = "First" });
            listView.Items.Add(new ListViewItem { Text = "Second" });
            listView.Items.Add(new ListViewItem { Text = "Third" });
            listView.Items.Add(new ListViewItem { Text = "Fourth" });
            view.SetListView(listView);
        }
    }
    interface IModel
    {

    }
    class Model : IModel
    {

    }
}

1 个答案:

答案 0 :(得分:1)

listView1只是一个变量,指向您在ListView中创建并添加到InitializeComponent控件的ListView控件。如果您为变量分配了新的ListView,则它对表单的Controls集合中的ListView控件没有任何影响。

将新实例分配给变量后,您在表单上看到的ControlslistView1集合中的原始实例,但listView1变量指向不同的您无法看到的实例,它只在内存中,ListView上的任何更改都不会显示给您。

如果您想为现有export default React.createClass({ getInitialState: function(){ return { url: null } }, componentDidMount: function(){ this.observeResource(); }, observeResource(){ var self = this function getValue(callback){ chrome.storage.local.get('newswireStoryUrl', callback); } getValue(function (url) { //here you are seting the state of the react component using the 'self' object self.setState({url: url.newswireStoryUrl}) //not sure if the 'return this.url' is needed here. //here the 'this' object is not refering to the react component but //the window object. return this.url; }); }, videoNotFound: function(){ return ( <div className="app"> <AppHeader /> <VideoNotFoundOverlay /> </div ) }, /* RENDER */ render: function(){ if(this.state.url == null){ return this.videoNotFound() } return ( <div className="app"> <AppHeader /> <VideoFoundOverlay /> </div> ) } }); 提供新项目,只需将新项目传递到表单,然后在清除之前的项目后添加这些新项目。