是否存在“数据类型”?可以存储组合框项目和列表框项目吗?
我想做的是将项目列表传递给方法而不是项目本身。
What I am trying to do is pass a list of items to a method rather than the item itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,所有东西都可以装箱到“对象”,并且它们可能更接近地共享一个层次结构父级(我面前没有文档,而且我认为这对本次讨论检查来说还不够重要)。
我想知道你到底想实现什么目标。如果您有一个
ComboBox
和一个ListBox
,它们都包含相同类型的对象(现在我们将它们称为字符串,但它应该适用于您想要的任何内容,我),您可以相对轻松地从ListBoxItem
或ComboBoxItem
中提取对象,然后传递List
> 或IEnumberable
或其他。如果它们不是相同的对象类型,无论出于何种原因,您可能需要检查您正在做什么以及为什么 - 需要组合两个完全不同的对象的两个列表并传递生成的串联列表的可能性相当小。
Well, everything can be boxed to 'Object' and they may share a hierarchical parent closer (I don't have the docs in front of me, and I don't think it's important enough to this discussion to check).
I'm wondering exactly what you're attempting to accomplish. If you have a
ComboBox
and aListBox
which both contain the same kind of objects (let's just call them strings, for now, but it should apply to anything you want, I'd think), you can, with relative ease, extract the object from theListBoxItem
or from theComboBoxItem
and then pass aroundList<string>
orIEnumberable<string>
or whatever.If they are not the same object type, for whatever reason, you may need to check what you're doing and why- the likelihood of needing to combine a two lists of two completely different objects and pass the resultant concatenated list around is fairly slim.
如果列表框项目的类型为
Car
,则它们的集合始终可以转换为IEnumerable
。如果您采用IEnumerable
作为方法的参数,您可以使用foreach
循环遍历项目或对其应用 linq 方法。如果您希望能够修改集合的内容,则必须更加具体,例如使用IList
。If your list box items are of type
Car
a collection of them can always be cast toIEnumerable<Car>
. If you take in anIEnumerable<Car>
to as parameter to the method you can loop through the items withforeach
or apply linq methods to it. If you want to be able to modify the collection's content you will have to be a bit more specific and e.g. use anIList<Car>
.在
System.Windows.Controls
中,ComboBoxItem
是一个ListBoxItem
。当然,一切都是对象。
In
System.Windows.Controls
aComboBoxItem
is aListBoxItem
.Of course, everything is an object.