如何获取绑定到控件的底层对象?

发布于 2024-12-21 13:59:00 字数 495 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2024-12-28 13:59:00

要在 WinForms 应用程序中添加对象数据源,请从菜单中选择“数据”>“数据源”。添加新数据源。在数据源配置向导中,选择对象,然后单击下一步。选择您想要用作数据源的类,然后单击完成。该类的公共属性现在应该出现在“数据源”窗口中。

然后创建该类的实例并将其分配给 DataSource 属性。例如:

private void Form1_Load(object sender, EventArgs e)
{
    MyClass myClass = new MyClass()
    {
        MyString = "aaaa"
    };

    myClassBindingSource.DataSource = myClass;
}

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 choose Object and click Next. Select the class you wish to use as a data source and click Finish. 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:

private void Form1_Load(object sender, EventArgs e)
{
    MyClass myClass = new MyClass()
    {
        MyString = "aaaa"
    };

    myClassBindingSource.DataSource = myClass;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文