简体   繁体   English

是否有可以存储组合框项目和列表框项目的“数据类型”?

[英]Is there a “data type” to that can store both Combobox items and ListBox items?

我想做的是将项目列表传递给方法,而不是项目本身。

If your list box items are of type Car a collection of them can always be cast to IEnumerable<Car> . 如果列表框项目的类型为Car ,则可以始终将它们的集合强制转换为IEnumerable<Car> If you take in an IEnumerable<Car> to as parameter to the method you can loop through the items with foreach or apply linq methods to it. 如果将IEnumerable<Car>用作方法的参数,则可以使用foreach所有项或对其应用linq方法。 If you want to be able to modify the collection's content you will have to be a bit more specific and eg use an IList<Car> . 如果您希望能够修改集合的内容,则必须更加具体一些,例如,使用IList<Car>

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 a ListBox 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 the ListBoxItem or from the ComboBoxItem and then pass around List<string> or IEnumberable<string> or whatever. 如果您有一个ComboBoxListBox都包含相同类型的对象(暂时将它们称为字符串,但我认为它应该适用于您想要的任何对象),则可以相对轻松地提取ListBoxItemComboBoxItem ,然后传递List<string>IEnumberable<string>或其他任何对象。

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. 如果它们不是同一对象类型,则无论出于何种原因,您都可能需要检查您在做什么以及为什么-需要组合两个完全不同的对象的两个列表并传递结果并置的列表的可能性很小。

ListItemCollection YourList = YourDropDownList.Items;

In System.Windows.Controls a ComboBoxItem is a ListBoxItem . System.Windows.ControlsComboBoxItemListBoxItem

Of course, everything is an object. 当然,一切都是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM