【问题标题】:.NET Designtime Datasource (for Combobox).NET 设计时数据源(用于组合框)
【发布时间】:2008-12-11 11:47:59
【问题描述】:

我正在尝试创建一个 ObjectDataSource,我可以使用它来绑定到一个 BindingSource,而后者应该绑定到一个 ComboBox。

我为这个类创建了一个简单的类和一个简单的列表(见下文)

  1. Times 列表类未显示在我的工具箱中,因此我无法将其拖到表单中,因此我可以选择它作为绑定源的数据源。
  2. 第二个选项是创建一个新的项目数据源(ObjectDataSource)。此处要求“选择您希望绑定到的对象”。我向 Form1 添加了一个朋友/公共/私有变量,它实例化了 Times 类。但是这个变量没有显示。我的项目命名空间中出现的唯一对象是 Form1。

我错过了什么?

Public Class Time
    Private _timeValue As String
    Private _timeDisplay As String

    Public Sub New(ByVal Value As String, ByVal Display As String)
        Me._timeDisplay = Display
        Me._timeValue = Value
    End Sub

    Public Property Display() As String
        Get
            Return Me._timeDisplay
        End Get
        Set(ByVal value As String)
            Me._timeDisplay = value
        End Set
    End Property

    Public Property Value() As String
        Get
            Return Me._timeValue
        End Get
        Set(ByVal value As String)
            Me._timeValue = value
        End Set
    End Property
End Class

Public Class Times : Inherits List(Of Time)
    Public Sub New()

    End Sub
End Class

【问题讨论】:

    标签: .net vb.net combobox objectdatasource design-time


    【解决方案1】:

    要改善使用 ObjectDataSource 的体验,请考虑使用 [DataObject] 标记您的数据类型。此外,还有一个[DataObjectMethod] 属性定义了可能的操作。

    【讨论】:

      【解决方案2】:

      我可以将System.ComponentModel.DataObject 属性添加到class。但是我无法将System.ComponentModel.DataObjectMethod 添加到我的Display/Value property。当我将它们更改为 Functions 时,我收到以下错误:

      '重载解析失败,因为没有可访问的New() 接受这个数量的参数'

      'This works
      <System.ComponentModel.DataObject()> _
      Public Class Time
          Private _timeValue As String
          Private _timeDisplay As String
      
          Public Sub New()
      
          End Sub
      
          Public Sub New(ByVal Value As String, ByVal Display As String)
              Me._timeDisplay = Display
              Me._timeValue = Value
          End Sub
      
          'This doesn't work
          <System.ComponentModel.DataObjectMethod()> _
          Public Function getDisplay() As String
              Return Me._timeDisplay
          End Function
      
          'This doesn't work
          <System.ComponentModel.DataObjectMethod()> _
          Public Function getValue() As String
              Return Me._timeValue
          End Function
      End Class
      

      【讨论】:

      • DataObjectMethod 没有无参数构造函数,添加一个System.ComponentModel.DataObjectMethodType
      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 2016-08-05
      • 2023-03-11
      • 1970-01-01
      • 2011-06-19
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多