将详细报告绑定到具有存储在字典中的属性的类模型(VB.NET,Devexpress)

时间:2015-11-20 02:10:54

标签: vb.net dictionary devexpress devexpress-windows-ui

我试过Devexpress的例子

Dim binding As New XRBinding("Text", dsProducts1, "Products.UnitPrice")

但我的模型没有在他们的班级中明确写出他们的属性。需要一个方法GetProperty(" column_name_here")来获取它的数据。我想知道XRBinding的第三个参数是否可以作为方法?像:

Dim binding As New XRBinding("Text", dsProducts1, product.GetProperty("name"))

其他信息:

我的所有模型类都扩展了这个Dao类,它负责获取数据库中的数据。 Dao类有一个受保护的变量作为Dictionary(Of String,Object)来存储数据库中的值(key =列名,值=列行值)。

现在,当我想在数据库中获取某些内容时,我只会调用

Dim user As New User // this class extends the Dao class

Dim userId = user.GetProperty("id") // Method to get the value from Dictionary, first parameter is the Dictionary key or column name from the DB

我做了这个,所以我不必创建每个模型类并设置该类的属性,因为它有点麻烦。

1 个答案:

答案 0 :(得分:0)

似乎无法绑定某些方法。我建议你看一下ExpandoObject动态课。可以在运行时添加此类的成员。此类实现IDictionary(Of String, Object)接口,您可以使用该接口从Dictionary(Of String, Object)生成属性。

以下是示例:

具有受保护Dao属性的基础Dictionary(Of String, Object)类实现示例:

Public Class Dao
    Private _values As Dictionary(Of String, Object)

    Public Sub New()
        _values = New Dictionary(Of String, Object)
    End Sub

    Public Overridable Sub Fill(index As Integer)
        _values.Clear()

        _values.Add("ID", index)
        _values.Add("Product", "Banana " & index)
        _values.Add("Price", 123.45 + index)
    End Sub

    Protected ReadOnly Property Values As Dictionary(Of String, Object)
        Get
            Return _values
        End Get
    End Property
End Class

具有Dao属性的DynamicValues类后代的示例,该属性基于ExpandoObject返回Dictionary(Of String, Object)(您必须省略属性类型):

Public Class DynamicDao
    Inherits Dao

    Private _dynamicValues As ExpandoObject

    Public Overrides Sub Fill(index As Integer)
        MyBase.Fill(index)

        _dynamicValues = New ExpandoObject()

        Dim keyValues = DirectCast(_dynamicValues, IDictionary(Of String, Object))

        For Each pair In Values
            keyValues.Add(New KeyValuePair(Of String, Object)(pair.Key, pair.Value))
        Next
    End Sub

    Public ReadOnly Property DynamicValues ' <= There is no type. In hint is displayed «As Object».
        Get
            Return _dynamicValues
        End Get
    End Property
End Class

DynamicDao中使用XtraReport类:

Dim list = New List(Of DynamicDao)

For index% = 0 To 9
    Dim dao = New DynamicDao()
    dao.Fill(index%)

    list.Add(dao)
Next

Dim labelID = New XRLabel()
labelID.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.ID"))

Dim labelProduct = New XRLabel()
labelProduct.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.Product"))
labelProduct.LeftF = 50

Dim labelPrice = New XRLabel()
labelPrice.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.Price"))
labelPrice.LeftF = 150

Dim detail = New DetailBand()
detail.Controls.Add(labelID)
detail.Controls.Add(labelProduct)
detail.Controls.Add(labelPrice)

Dim report = New XtraReport()
report.Bands.Add(detail)
report.DataSource = list

report.ShowRibbonPreview()