Reputation: 31
I want to add a List to ComboBox through Dispatcher.BeginInvoke. But when I try to put it in a loop, only last value is loading.
private void LoadToComboBox(List<string> msg)
{
for (int i = 0; i < msg.Count; i++)
{
this.Dispatcher.BeginInvoke(() => cmbListItems.Items.Add(msg[i]));
}
}
Upvotes: 1
Views: 357
Reputation: 2236
The Dispatcher.BeginInvoke() is an asynchronous call. What is happening is that by the time you are calling your cmbListItems.Items.Add()
function, it is already set to msg.Count
.
Try something like this:
private void LoadToComboBox(List<string> msg)
{
this.Dispatcher.BeginInvoke(() =>
{
for (int i = 0; i < msg.Count; i++) {
cmbListItems.Items.Add(msg[i]);
}
});
}
Upvotes: 2