我有一個綁定到我的控件內的對象的BindingSource。表單在運行時從列表中生成了很多文本框。簡化的代碼是這樣的:Winforms數組綁定到數組或列表中的特定元素
public class MyClassname: INotifyPropertyChanged
{
// some more properties here ...
public BindingList<string> mylist{ get; set; }
}
// ...
public MyClassname myclassname = mew MyClassname();
private BindingSource bs = new BindingSource();
// ...
bs.DataSource = typeof(myclassname);
bs.Add(myclassname);
// ...
textBox.DataBindings.Add("Text", bs, "mylist[2]");
我收到以下錯誤:
DataMember property "mylist[2]" cannot be found on the DataSource
我已經嘗試過多種類型,如字典,數組或列表,並試圖訪問它以各種方式沒有任何成功。
如果我直接綁定到該元素是這樣的:只有
textBox.DataBindings.Add("Text", myclassname.mylist[2], "");
它的工作原理,但一種方式。 (在其他地方修改時不會更新文本框)
Ps。即使嵌套對象,也可以正確訪問其他成員。
你是否找到了解決這個問題的方法?我嘗試使用DevExpress LayoutControl來做同樣的事情,並且我也動態地創建控件。 – 2014-11-24 07:02:17
我試過了你的「工作」示例,但它並沒有在更改文本框中的文本後更新列表的值。綁定似乎是相同的textBox.Text = items [0];沒有任何機會更新價值。 – 2014-11-24 07:11:39
@Verarind嗨我搜索了幾個星期的網絡,並嘗試了一百萬的東西。最後,我不得不使用我的文章中提到的直接綁定,失去了BindingSource的功能。爲此,您將需要使用BindingList或實現[此處在MSDN上描述的INotifyPropertyChanged接口](http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged%28v=vs.110 %29.aspx)如果您使用常規列表,它將只是一種方式。 – wwr 2014-11-25 09:13:45