【问题标题】:Component1: How to Add DictionaryEntry object to C1List as item?Component1:如何将 DictionaryEntry 对象作为项添加到 C1List?
【发布时间】:2018-04-18 10:32:53
【问题描述】:

我必须用现有应用程序中的组件一 C1List 替换 .net Listbox。以前它的项目添加如下。

 lstFrmType.Items.Add(New DictionaryEntry("False", 
                                   FormatPercent(-0.1234, 
                                   CInt(numFrmDecimalPlaces.Value), 
                                   TriState.True, 
                                   TriState.False, 
                                   TriState.False)))

但是对于组件一 C1list,我可以看到它有新的方法 AddItem() 但它只接受参数作为字符串。我无法添加 DictionaryEntry 对象。

 lstFrmType.AddItem(New DictionaryEntry("False", 
                                   FormatPercent(-0.1234, 
                                   CInt(numFrmDecimalPlaces.Value), 
                                   TriState.True, 
                                   TriState.False, 
                                   TriState.False)))

还有其他方法可以实现吗?

【问题讨论】:

    标签: vb.net componentone


    【解决方案1】:

    在未绑定模式下使用C1List 时存在某些限制(AddItem 只能在未绑定模式下使用)。在未绑定模式下,您不能使用DisplayMember/ValueMember,您肯定需要在这里使用您的DictionaryEntry 对象。因此最好使用绑定模式(DataMode = Normal)。您可以编写一个看起来好像我们正在使用AddItem 的扩展,但在幕后您可以将数据推送到列表的DataSource

    Imports System.Runtime.CompilerServices
    Imports C1.Win.C1List
    Imports System.ComponentModel
    
    Module C1ListExtensions
        <Extension()>
        Public Sub AddItem(ByVal list As C1List, ByVal item As DictionaryEntry)
            If list.DataMode = DataModeEnum.AddItem Then
                Throw New Exception("Need DataMode to be DataMode.Normal")
            Else
                If list.DataSource Is GetType(BindingList(Of Tuple(Of Object, Object))) Then
                    ' Set DisplayMember and ValueMember to Item1 and Item2
                    DirectCast(list.DataSource, BindingList(Of Tuple(Of Object, Object))).Add(New Tuple(Of Object, Object)(item.Key, item.Value))
                Else
                    Throw New Exception("Implement your own DataSource here")
                End If
            End If
        End Sub
    End Module
    

    此方法的唯一限制是您必须根据您的 DataSource 类型实现此扩展。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2014-09-14
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2018-11-04
      • 2020-04-22
      • 2021-07-25
      相关资源
      最近更新 更多