C# WinForms でaをaBindingListとして使用しようとしていますが、アイテムを に追加しようとすると、スローされます。次のコードは問題を示しています (フォームに があると仮定します)。DataSourceListBoxBindingListArgumentOutOfRangeExceptionListBox listBox1
BindingList<string> dataSource = new BindingList<string>();
listBox1.DataSource = dataSource;
dataSource.Add("Test1"); // Exception, here.
dataSourceすでにアイテムが含まれている場合、例外は発生しないことに注意してください。
BindingList<string> dataSource = new BindingList<string>();
dataSource.Add("Test1");
listBox1.DataSource = dataSource;
dataSource.Add("Test2"); // Appears to work correctly.
DataSourceアイテムを追加する前にプロパティを設定し、後でnull再設定することで問題DataSourceを回避できますが、これはハックのように感じるので、そうしないようにしたいと思います。
アイテムを追加しても例外がスローされないように、空を使用する(ハックではない)方法はDataSourceありますか?ListBox
編集:スタックトレース:
System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int 値) + 0x1ec バイト
System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(オブジェクト送信者、System.EventArgs e) + 0x2e バイト
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x39 バイト
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition、bool Validating、bool endCurrentEdit、bool firePositionChange、bool pullData) + 0x14f バイト
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(オブジェクト送信者、System.ComponentModel.ListChangedEventArgs e) + 0x2e4 バイト
System.dll!System.ComponentModel.BindingList.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x17 バイト
System.dll!System.ComponentModel.BindingList.FireListChanged(System.ComponentModel.ListChangedType タイプ、int インデックス) + 0x35 バイト
System.dll !System.ComponentModel.BindingList.InsertItem(int インデックス, System._ Canon アイテム) + 0x3f バイト
mscorlib.dll!System.Collections.ObjectModel.Collection.Add( System._Canon アイテム) + 0x76 バイト