I have a problem with a list, which is bound to a ComboBox.
The List:
private List<string> _CourseList = new List<string>();
public List<string> CourseList
{
get { return _CourseList; }
set
{
_CourseList = value;
OnPropertyChanged("CourseList");
}
}
XAML code of the ComboBox:
<ComboBox x:Name="cbxCourse" Height="23" MinWidth="100" Margin="5,1,5,1" VerticalAlignment="Top" ItemsSource="{Binding Path=CourseList}" IsEnabled="{Binding Path=CanExport}" SelectedIndex="{Binding Path=CourseListSelectedIndex}" SelectedItem="{Binding Path=CourseListSelectedItem}" SelectionChanged="cbxCourse_SelectionChanged"/>
Now i fill the List from another thread:
void Database_LoadCompleted(object sender, SqliteLoadCompletedEventArgs e)
{
foreach (DataTable Table in DataSetDict[CampagneList[0]].Tables)
{
CourseList.Add(Table.TableName);
}
}
Everything looks good, and the ComboBox changed its items.
When I try to update the ComboBox (CourseList) in the MainThread with:
private void cbxCampagne_SelectionChanged(object sender, EventArgs e)
{
if (cbxCampagne.SelectedItem != null)
{
CourseList.Clear();
foreach (DataTable Table in DataSetDict[CampagneList[_CampagneListSelectedIndex]].Tables)
{
CourseList.Add(Table.TableName);
}
}
all Elements of CourseList changed (I can see it in a Textbox) but in the ComboxBox nothing happens.
Any ideas?
Try changing
CourseListanObervableCollection<T>http://msdn.microsoft.com/en-us/library/ms668604.aspx
The Binding only notifies the UI when
CourseListis set (when the list is assigned) and not when its contents change.Some Code from Invoke event on MainThread from worker thread
This demonstrates that the
List<>will not change once assigned and theObservableList<>will change.ViewModel
Window
This can be easily modified to use the Dispatcher Invoke: Change WPF controls from a non-main thread using Dispatcher.Invoke