如何使用设计器中的bindingsource对象将数据正确绑定到组合框。 [英] How to correctly bind data to combobox using bindingsource object in the designer.

查看:76
本文介绍了如何使用设计器中的bindingsource对象将数据正确绑定到组合框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:



我理解如何以编程方式将数据绑定到ComboBox:



例如:



Hello:

I understand how to bind data to a ComboBox programmatically:

For Example:

ComboBox1.DataSource = data.EmployeeList()
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "EmployeeID"





但是,我试图做的是绑定数据使用设计器和绑定源对象。



如下所示,使用设计器我添加了数据源并配置了显示和值成员,但表单加载时没有数据。



如何使用设计器将列表绑定到Combobox?



我尝试过:



首先我创建了一个数据源:





However, what I am attempting to do is bind data using the designer and a Binding Source Object.

As demonstrated below, using the designer I added a data source and configured the display and value member but there is no data when the form loads.

How do I bind a list using the designer to a Combobox?

What I have tried:

First I created a Data Source:

Public Class Employee

    Public Property EmployeeID As String
    Public Property Name As String

End Class







Public Class Data

    Public Function Data() As List(Of Employee)

        Return New List(Of Employee)() From { _
         New Employee() With { _
          .Name = "George Benson", _
          .EmployeeID = 321
         }, _
         New Employee() With { _
          .Name = "Gladys Knight", _
          .EmployeeID = 322
         }, _
         New Employee() With { _
          .Name = "David Bowie", _
          .EmployeeID = 323
         }, _
         New Employee() With { _
          .Name = "Steve Wonder", _
          .EmployeeID = 324
         }, _
         New Employee() With { _
          .Name = "Tina Turner", _
          .EmployeeID = 325
         }
        }
    End Function

End Class







接下来我创建了一个带有一个只读属性的View Model,这样我就可以绑定

ComboBox吧。



注意:这不是MVP或MVPM的完全实现,这仅用于演示。






Next I created a View Model with a single read only property so that I can bind the
ComboBox to it.

Note: this is not full implementation of MVP Or MVPM, this is for demonstration only.

Public Class EmployeeViewModel

    Private _listEmployees As Data

    Sub New()
        _listEmployees = New Data()
    End Sub

    Public ReadOnly Property EmployeeList() As List(Of Employee)
        Get
            Return _listEmployees.Data()
        End Get
    End Property

End Class

推荐答案

我在一篇文章中有一个示例项目,用ListBox做你想做的事,但是ComboBox也是一样的:在C#中使用JSON& VB [ ^ ]下载代码(v1.3)并查看VB版 WinFormSimpleCollection 项目。这是作为MVVM-esque应用程序完成的。它包括这个小帮助函数,用于将模型列表绑定到控件:

I have a sample project in an article that does what you want with a ListBox, but the ComboBox is just the same: Working with JSON in C# & VB[^] Download the code (v1.3) and look at the VB version of WinFormSimpleCollection project. This is done as a MVVM-esque app. It includes this little helper function for binding the list of models to the control:
Public Shared Sub BindList(Of TModel)(ctrl As ListControl, dataSource As IList(Of TModel), displayMember As String, valueMember As String)

    ctrl.DisplayMember = displayMember
    ctrl.ValueMember = valueMember
    ctrl.DataSource = dataSource

End Sub



< b>更新:这也可能有所帮助: c# - WinForms。设计时数据绑定到子控件的属性 - Stack Overflow [ ^ ]


Update: this may also be helpful: c# - WinForms. Design-time Data binding to child control's property - Stack Overflow[^]


这是一篇很好的文章,描述了绑定数据的几种方法:详细的数据绑定教程 [ ^ ]
Here is an excellent article describing several ways to bind data: A Detailed Data Binding Tutorial[^]


这篇关于如何使用设计器中的bindingsource对象将数据正确绑定到组合框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆