ListBox の SelectedItem データをプロパティにバインドしようとしています。次のコードは例です。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BindingFailure
{
static class Program
{
class OuterObject
{
public string selected { get; set; }
public List<string> strings { get; set; }
}
public static void Main()
{
List<OuterObject> objs = new List<OuterObject>()
{
new OuterObject(), new OuterObject()
};
objs[0].strings = new List<string> { "one", "two", "three" };
objs[1].strings = new List<string> { "four", "five", "six" };
Form form = new Form();
BindingSource obs = new BindingSource(objs, null),
ibs = new BindingSource(obs, "strings");
BindingNavigator nav = new BindingNavigator(obs);
ListBox lbox = new ListBox();
lbox.DataSource = ibs;
lbox.DataBindings.Add(new Binding("SelectedItem", obs, "selected"));
form.Controls.Add(nav);
form.Controls.Add(lbox);
lbox.Location = new System.Drawing.Point(30, 30);
Application.Run(form);
}
}
}
アイテムを選択して先に進み、アイテムを選択してから終了すると、期待どおりに機能します。しかし、ナビゲーターを使用して外側の 2 つのオブジェクトを切り替えると、選択した項目が誤った値で上書きされているように見えます。BindingNavigator が要素に移動するたびに、ListBox はそのコレクションの最初の項目に移動するように指示され、その SelectedItem にバインドされた変数にあった値を上書きするようです。
これを修正する方法についてのアイデアはありますか? 前もって感謝します。
編集: これは、デバッグ バイナリを含むサンプル プロジェクトのアーカイブです。
http://www.mediafire.com/?dzmqmz0mynj
編集:これは、受け入れられた回答に基づくヘルパー関数です。
public static void Bind(ListControl list, BindingSource outersource, string dataMember)
{
Binding bindSel = new Binding("SelectedItem", outersource, dataMember);
list.DataBindings.Add(bindSel);
outersource.CurrentChanged += delegate
{
list.BeginInvoke(new MethodInvoker(bindSel.ReadValue));
};
}