listbox Refresh() in c#

前端 未结 10 2349
梦毁少年i
梦毁少年i 2020-12-30 10:54
int[] arr = int[100];
listBox1.DataSource = arr;
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
    .....//some processes
    listBox1.DataSource =         


        
相关标签:
10条回答
  • 2020-12-30 11:24

    ListBox only updates its shown content when the object that is binded on dataSource notifys it own changes. the BindingSource object has an event called DataSourceChanged. when the Source is changed to a different object the Listbox will update itself. Same thing when you bind a List. Nothing will happen if you change the List, because the List doesn't notify that it has been changed. There is a Simple solution for this Problem: use BindingList http://msdn.microsoft.com/de-de/library/ms132679%28v=vs.110%29.aspx

    the BindingList has the ListChanged Event is called every time when the List is changed (obviously). So the DataBindings of Windows.Form objects use events like ListChanged to update themselves. A simple List doesn't support this event.

    SO if you want to work with a lot of Data Bindings you should know about: http://msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

    0 讨论(0)
  • 2020-12-30 11:24

    Windows forms to see changes especially on Listbox and other controls before load is finished is tricky. To see data as its loaded use invalidate(); then Update();

    0 讨论(0)
  • 2020-12-30 11:26

    Managed to do just with

    FirstListBox.DataContext = null;
    FirstListBox.DataContext = App.ViewModel;
    

    Simply loses link and get all the data back to it.

    0 讨论(0)
  • 2020-12-30 11:27

    well, without binding I only managed with:

    this.Hide();
    this.Show();
    

    it redraws everything...

    0 讨论(0)
  • 2020-12-30 11:27

    These links might help.

    How can I update a listbox item (C#)? - http://arstechnica.com/civis/viewtopic.php?f=20&t=554717

    Bind ArrayList to ListBox - http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/BindArrayListtoListBox.htm

    0 讨论(0)
  • 2020-12-30 11:31

    I inherited ListBox and added a public method calling RefreshItems() which does what we want. Already implemented and all. I dont know why they didnt put in a public method.

    0 讨论(0)
提交回复
热议问题