[英]Winforms DataBinding works one way only
我通過以下方式進行數據綁定
private List<MyEditor> Editors { get; set; }
private Dictionary<int,object> dictionary
private void SetEditors()
{
Editors.Clear();
foreach (var element in dictionary)
{
var editor = MyFactory.GetEditor();
editor.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
editor.DataBindings.Add("Value", element, "Value");
//some code
Editors.Add(editor);
}
//some more code
}
然后在GUI中對Editors[0]
Value
做一些更改,然后在另一段代碼中,即使我使用Editors[0].DataBindings["Value"].WriteValue()
,我也嘗試從dictionary
元素中獲取值並發現它沒有改變。 Editors[0].DataBindings["Value"].WriteValue()
以確保將數據寫入dictionary
。
在調試器中,我可以看到以下圖片:
Editors[0].DataBindings["Value"] {System.Windows.Forms.Binding} System.Windows.Forms.Binding
....
DataSource {[0, oldValue]} object {System.Collections.Generic.KeyValuePair<int,object>}
....
而
Editors[0].Value "newValue" object {string}
會是什么呢? 將不勝感激任何想法。
我不確定我是否正確遵循,但是我相信答案是,當您枚舉Dictionary時,您得到的KeyValuePair(KVP)對象是在枚舉時創建的。 再次枚舉它,您將獲得另一組對象。 這是因為KVP是值類型。
要更改KVP引用的內容,您必須返回到原始的Dictionary對象。 數據綁定不會這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.