textBoxのデータをエントリにバインドしたいと思いDictionary<string,string>ます。私はデータバインディングを介してこれを達成しようとしているので、ユーザーがtextBoxを編集した後にコンテンツが更新されます。
これは私が行ったことのデモコードです:
Nameと辞書を持つclassA List:
class ClassA
{
public string Name { get; set; }
public Dictionary<string, string> List { get; set; }
public ClassA()
{
this.Name = "Hello";
this.List = new Dictionary<string, string>
{
{"Item 1", "Content 1"},
{"Item 2", "Content 2"}
};
}
}
フォームでは、私はにバインドtextBox1します:NametextBox2List["Item 1"]
ClassA temp = new ClassA();
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add("Text", temp, "Name");
textBox2.DataBindings.Add("Text", temp.List["Item 1"], "");
label1.DataBindings.Add("Text", temp, "Name");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
label1.Text = temp.List["Item 1"];
}
textBox1テキストを変更すると、label1コンテンツ(Name)が正常に更新されます。
ただし、textBox2テキストを変更すると、label1コンテンツには元のList["Item 1"]値が表示されます。
どうすれば正しくバインドできtextBox2ますList["Item 1"]か?