简体   繁体   中英

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> . 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. 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> .

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.

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 .

Of course, everything is an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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