[英]listbox Refresh() in c#
int[] arr = int[100];
listBox1.DataSource = arr;
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
.....//some processes
listBox1.DataSource = null;
listBox1.DataSource = arr;
}
is not working,
還,
listBox1.Refresh(); is not working,
還,
listBox1.Update(); is not working,
我知道我可以使用BindingList<T>
但我必須只使用數組。
你能幫我如何刷新列表框嗎?
我在這里的堆棧交換的第一個答案。
C#.Net 4.0:
listBox1.DataSource = null;
listBox1.DataSource = names;
我注意到第一次設置數據源,它會刷新。 當它被設置,並且您嘗試再次將它設置為同一個時,它不會更新。
所以我把它設為null,把它設置為同一個,並且它正確地顯示給我這個問題。
當在dataSource上綁定的對象通知它自己的更改時,ListBox僅更新其顯示的內容。 BindingSource對象有一個名為DataSourceChanged的事件。 當Source更改為其他對象時,Listbox將自行更新。 綁定List時也是如此。 如果更改List,則不會發生任何事情,因為List不會通知它已被更改。 這個問題有一個簡單的解決方案:使用BindingList http://msdn.microsoft.com/de-de/library/ms132679%28v=vs.110%29.aspx
每次更改List時(顯然)都會調用BindingList並調用ListChanged事件。 因此,Windows.Form對象的DataBindings使用ListChanged之類的事件來更新自己。 一個簡單的List不支持此事件。
因此,如果您想使用大量數據綁定,您應該了解: http : //msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx
管理只做
FirstListBox.DataContext = null;
FirstListBox.DataContext = App.ViewModel;
簡單地丟失鏈接並將所有數據返回給它。
我繼承了ListBox
並添加了一個名為RefreshItems()
的公共方法,它RefreshItems()
我們的需要。 已經實施了所有。 我不知道為什么他們沒有采用公共方法。
問題可能來自ListBox SelectionMode。
由於我不知道的原因,當SelectionMode是SelectionMode.None時,數據綁定不起作用。
解決方法可能是:
listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.DataSource = myDatasource;
listBox.SelectionMode = SelectionMode.None;
希望能幫助到你。
嘗試以下方法
listBox1.DataBind()
這些鏈接可能有幫助。
如何更新列表框項(C#)? - http://arstechnica.com/civis/viewtopic.php?f=20&t=554717
將ArrayList綁定到ListBox - http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/BindArrayListtoListBox.htm
好吧,沒有約束我只管理:
this.Hide();
this.Show();
它重繪了一切......
Windows窗體在加載完成之前查看更改,特別是在Listbox和其他控件上是很棘手的。 要查看數據作為其加載使用invalidate(); 然后Update();
使用應該解決它的BeginUpdate和EndUpdate。 無需兩次設置數據源
listBox1.BeginUpdate();
listBox1.DataSource = myList;
listBox1.EndUpdate();
BindingSource bs = new BindingSource();
bs.DataSource = arr;
listbox1.DataSource = bs;
arr[0] = 100; //Do some value change
bs.ResetBindings(false); //Refresh the listbox
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.