问题
Possible Duplicate:
C# Force ListBox to update elements
Let's consider this small piece of code:
listbox.DataSource = base_items;
listbox.DisplayMember = "Name";
// ... a bit later in the program
base_items.Add( new Base_Item("Unnamed") );
From this point, how do I do to make the listbox update its items? The only way for me to see the update is to close the window and reload it again.
回答1:
Just remove and add databinding again. You can create method that can be used on first load and when new item was added:
void BindData()
{
listBox.DataSource = null;
listBox.DataSource = base_items;
listbox.DisplayMember = "Name";
}
So here is the code for adding new item and refreshing listbox:
base_items.Add(new Base_Item("Unnamed"));
BindData();
回答2:
As mentioned in Marc G's answer.
C# Force ListBox to update elements
If you know that the list needs refreshing, simple update the DisplayMember
of the listbox.
listbox.DisplayMember = "";
listbox.DisplayMember = "Name";
来源:https://stackoverflow.com/questions/13998587/how-refresh-items-of-a-listbox