【发布时间】:2012-12-19 06:08:50
【问题描述】:
我对绑定到ComboBox 的列表有疑问。
List:
private List<string> _CourseList = new List<string>();
public List<string> CourseList
{
get { return _CourseList; }
set
{
_CourseList = value;
OnPropertyChanged("CourseList");
}
}
ComboBox 的 XAML 代码:
<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"/>
现在我从另一个线程填写List:
void Database_LoadCompleted(object sender, SqliteLoadCompletedEventArgs e)
{
foreach (DataTable Table in DataSetDict[CampagneList[0]].Tables)
{
CourseList.Add(Table.TableName);
}
}
一切看起来都不错,ComboBox 改变了它的项目。
当我尝试更新 MainThread 中的ComboBox (CourseList) 时:
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);
}
}
CourseList 的所有元素都已更改(我可以在 Textbox 中看到它),但在 ComboxBox 中没有任何反应。
有什么想法吗?
【问题讨论】:
-
只是为了排除,尝试从组合框 xaml 中取消绑定 CourseSelectedItem 和 CourseSelectedIndex,看看会发生什么。有时同时设置这两个属性时,事情会变得很奇怪。
标签: c# wpf list binding combobox