I do DataBinding in the following way
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
}
Then in GUI I do some changes to Value of Editors[0], after that in another piece of code I try to get value from dictionary element and find out that it hasn’t changed, even though I use Editors[0].DataBindings["Value"].WriteValue() to ensure data is written to dictionary.
In debugger I can see the following picture:
Editors[0].DataBindings["Value"] {System.Windows.Forms.Binding} System.Windows.Forms.Binding
....
DataSource {[0, oldValue]} object {System.Collections.Generic.KeyValuePair<int,object>}
....
while
Editors[0].Value "newValue" object {string}
What could it be? Will be grateful for any ideas.
I’m not certain I’m following correctly, but I believe the answer is that when you enumerate a Dictionary, the KeyValuePair (KVP) objects you get are created at the time of the enumeration. Enumerate it again and you will get a different set of objects. This is because a KVP is a value type.
To alter something referred to by a KVP, you have to return to the original Dictionary object. A data binding does not do that.