0

我想用 WPF 中的列表填充组合框内容。我可以在winform中正常执行,但wpf看起来有点不同..

我不在 XAML 中创建任何代码。整个控件在运行时动态创建..

所以这是我的代码

cmbKriterKategori 是一个组合框。

                cmbKriterKategori.DisplayMemberPath = "Value";
                cmbKriterKategori.SelectedValuePath = "Key";
                cmbKriterKategori.ItemsSource = yHelper.GetCriterList().ToList();

发生错误

在使用 ItemsSource 之前,项目集合必须为空。

我也试过这样

                cmbKriterKategori.DisplayMemberPath = "Value";
                cmbKriterKategori.SelectedValuePath = "Key";
                cmbKriterKategori.DataContext = yHelper.GetCriterList().ToList();

它没有出现任何错误,但组合框没有任何项目..

yHelper.GetCriterList().ToList(); 此函数返回 List> 并且 yHelper.GetCriterList() 返回 Dictionary

我在winform中使用了该代码,它可以工作..

                cmbKriterKategori.DisplayMember = "Value";
                cmbKriterKategori.ValueMember ="Key";
                cmbKriterKategori.DataSource = yhelper.GetCriterList().ToList();

那么,问题是什么?

4

1 回答 1

0

你必须清除你的组合项目,这样Items collection must be empty before using ItemsSource就不会抛出异常

        cmbKriterKategori.Items.Clear();
        cmbKriterKategori.DisplayMemberPath = "Value";
        cmbKriterKategori.SelectedValuePath = "Key";
        cmbKriterKategori.ItemsSource = yHelper.GetCriterList().ToList();
于 2012-08-14T01:05:07.440 回答