0

コンボボックスのコンテンツをWPFのリストで埋めたい。私は通常winformでそれを行うことができますが、wpfは少し異なって見えます。

XAMLでコードを作成しません。コントロール全体が動的に実行時に作成されます。

これが私のコードです

cmbKriterKategoriはコンボボックスです。

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

エラーが発生します

ItemsSourceを使用する前に、Itemsコレクションを空にする必要があります。

そして私もそのように試みました

                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 に答える