如何获取绑定到控件的底层对象?
使用 WinForms 和数据绑定。
我有一个包含 BindingSource 组件的表单,并且已将设计器中的 DataSource 属性设置为一个类:
public class MyClass
{
public string MyString {get;set;}
}
现在,如何从源代码中获取分配给 BindingSource 的 MyClass 对象?
我尝试将 BindingSource 的 DataSource 属性转换为 MyClass,但不起作用。
附加说明
我的问题似乎是我从 WinForms Designer 设置数据源。
然后将 DataSource 设置为 MyClass 类型,而不是实际对象。
那么,是否创建了一个我可以从代码访问和修改的对象,以便有界对象的值显示在表单的控件上?
谢谢。
Using WinForms and data bindings.
I have a form containing a BindingSource component and I have set the DataSource property from the designer to a class:
public class MyClass
{
public string MyString {get;set;}
}
Now, how do I get the MyClass object assigned to the BindingSource from source code?
I've tried casting the DataSource property of the BindingSource to MyClass, not working.
Additional notes
My problem seems to be that I set the DataSource from the WinForms Designer.
The DataSource is then set to the type MyClass and not an actual object.
So, is there an object created that I can access and modify from code so that values from the bounded object shows on the form's controls ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在 WinForms 应用程序中添加对象数据源,请从菜单中选择“数据”>“数据源”。添加新数据源。在数据源配置向导中,选择
对象
,然后单击下一步
。选择您想要用作数据源的类,然后单击完成
。该类的公共属性现在应该出现在“数据源”窗口中。然后创建该类的实例并将其分配给
DataSource
属性。例如:To add an object data source in a WinForms application, from the menu choose
Data > Add New Data Source
. In the Data Source Configuration Wizard chooseObject
and clickNext
. Select the class you wish to use as a data source and clickFinish
. The public properties of the class should now appear in the Data Sources window.Then after you do that create an instance of the class and assign it to the
DataSource
property. For example: