在没有数据源的 ComboBox 上设置 DisplayMember 和 ValueMember [英] Set DisplayMember and ValueMember on ComboBox without DataSource
问题描述
我想在 ComboBox
上有一个 DisplayMember
和一个 ValueMember
,它只有 4 个值,而且它们总是相同的.
I would like to have a DisplayMember
and a ValueMember
on a ComboBox
which has only 4 values and they are always the same.
是否可以不使用 DataTable
作为 DataSource
并且不创建类?
Is it possible without using a DataTable
as DataSource
and without creating a class?
我想要类似的东西:
ValueMember= "Fixed"
DisplayMember= "Specific and unique number"
ValueMember= "Multiple"
DisplayMember= "Multiple and different numbers"
ValueMember= "Repeated"
DisplayMember= "One number repeated x times"
推荐答案
从根本上说,你不能为所欲为:
Fundamentally, you cant do what you want:
ValueMember= "Fixed"
DisplayMember= "Specific and unique number"
Value-
和 DisplayMember
不是用于指定文字值,而是用于指示属性名称 在别的地方(比如一个班级).
Value-
and DisplayMember
are not for specifying the literal values, but are used to indicate the Property Names in something else (like a class).
不使用 DataSource
(标题)与不使用类(问题文本)不同.有创建类的替代方法:
Without using a DataSource
(title) is not the same as not using a class (question text). There are alternatives to creating a class:
您可以使用现有的 NET KeyValuePair
类将值与名称链接:
You could use the existing NET KeyValuePair
class to link a value with a name:
cbox.Items.Add(New KeyValuePair(Of String, String)("Specific",
"Specific and unique number"))
cbox.Items.Add(New KeyValuePair(Of String, String)("Multiple",
"Multiple and different numbers"))
cbox.Items.Add(New KeyValuePair(Of String, String)("Repeated",
"One number repeated x times"))
cbox.ValueMember = "Key"
cbox.DisplayMember = "Value"
没有数据源 - 数据在项目集合中.还有 Tuple
如另一个答案中所述
There is no DataSource - the data is in the items collection. There is also Tuple
as explained in another answer
使用一个字符串作为另一个字符串的键是很奇怪的.通常在代码中,您会想要一些不太容易因拼写错误而出错的东西.键入Fized"某处破坏了您的代码.Enum
更有意义:
Using one string as the key for another string is a quite odd. Typically in code you would want something less prone to errors from typos. Typing "Fized" somewhere breaks your code. An Enum
makes much more sense:
Private Enum ValueStyle
Specific = 0
Multiple = 1
Repeated = 2
End Enum
现在,您可以创建一个 List
链接用户描述和 Enum
常量:
Now, you can create a List
which links a description for the user and the Enum
constants:
' fuller text descr of the enum for the user
Dim descr As String() = {"Specific and unique number",
"Multiple and different numbers",
"One number repeated x times"}
' get enum values into an array of ValueStyle
Dim values = [Enum].GetValues(GetType(ValueStyle)).Cast(Of ValueStyle).ToArray
' create a List of anon objects from the descr() and values()
Dim lst = values.Select( Function (q) New With
{.Value = q, .Name = descr (q)}
).ToList()
cboPicker.ValueMember = "Value"
cboPicker.DisplayMember = "Name"
cboPicker.DataSource = lst
这将创建一个匿名类型 - 一个没有类的对象 - 具有映射到枚举和描述数组的 Name 和 Value 属性.如果 Enum
值不是连续的(例如 {8, 65, 99}),则必须以不同的方式构建列表.
This creates an Anonymous Type - an object without a class - with a Name and Value property mapped to the Enum and description array. If the Enum
values are not sequential (e.g. {8, 65, 99}), the list would have to be built differently.
这会创建一个匿名类型对象的临时集合并将其分配为数据源.您将无法访问其他方法中的 Name
和 Value
属性,因为无法将匿名类型传递给其他方法.但是用户将看到所需的文本,NET/VB 将提供该枚举作为 SelectedValue
的值.使用 SelectedValue
更改事件:
This creates a temporary collection of Anonymous Type objects and assigns it as the DataSource. You wont be able to access the Name
and Value
properties in other methods because anonymous type cant be passed to other methods. But the user will see the desired text and NET/VB will provide the value as that enum as the SelectedValue
. Using the SelectedValue
changed event:
' name user sees == cboPicker.Text
' value == cboPicker.SelectedValue boxed as Object
Dim userChoice As ValueStyle = CType(cboPicker.SelectedValue, ValueStyle)
If userChoice = ValueStyle.Specific Then
'...
ElseIf userChoice = ValueStyle.Repeated Then
'...
End If
请注意,不是测试Fixed"作为字符串,代码使用枚举,但仍然可读.
Notice that rather than testing "Fixed" as a string, the code uses the enum but is still every bit as readable.
MSDN:匿名类型 (Visual Basic)
MSDN: Anonymous Types (Visual Basic)
那些符合不需要新课程的标准,但请考虑:
Those fit the criteria of not needing a new class, but consider:
Friend Class NameValuePair
Public Property Name As String
Public Property Value As Int32
Public Sub New(n As String, v As Int32)
Name = n
Value = v
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
该类非常简单,几乎可以无限地重用,将任何Name
与任何Value
相关联.它可以在任意数量的项目中与任意数量的基于列表的控件一起使用.创建和使用它们的列表的代码比使用其他方法更简单.
The class is very simple and is almost infinitely reusable in associating any Name
with any Value
. It could be used with any number of list based controls, in any number of projects. The code to create and use a list of them is simpler than using the other methods.
这篇关于在没有数据源的 ComboBox 上设置 DisplayMember 和 ValueMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!