この Winforms プロジェクトに問題があります。次の方法で辞書propListが取り込まれたcombBoxでSelectedIndexChangedイベントを使用しようとしています。
comboBox1.DataSource = new BindingSource(propList, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
これはイベント自体です:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
//comboBox.DataSource = null;
System.Collections.Generic.KeyValuePair<int, string> dummy =
(System.Collections.Generic.KeyValuePair<int, string>)
comboBox.SelectedItem;
//comboBox1.DataSource = new BindingSource(propList, null);
PopulateCollars(dummy.Key);
}
次に、これを KeyValuePair キャストでスローします。
Items collection cannot be modified when the DataSource property is set
デバッグが次のことを示しているため、これが適切なキャストであることはわかっています。
SelectedItem ~~~ object System.Collections.Generic.KeyValuePair<int,string>}
私の質問は、明示的なキャストがアイテム コレクションを変更するのはなぜですか? キャストがキャストしているデータを変更しない C++ で育ったので、これは私には意味がありません。
注意として、コメント アウトされた行を使用すると、明らかにデータ ソースを null に設定すると、comboBox 内のすべてのメンバーが消去されるため、null 参照例外が発生します。