迭代 CompositeCollection 的项目

发布于 2024-09-05 06:43:00 字数 962 浏览 8 评论 0原文

考虑代码:

ObservableCollection<string> cities = new ObservableCollection<string>();
ObservableCollection<string> states = new ObservableCollection<string>();

ListBox list;

cities.Add("Frederick");
cities.Add("Germantown");
cities.Add("Arlington");
cities.Add("Burbank");
cities.Add("Newton");
cities.Add("Watertown");
cities.Add("Pasadena");

states.Add("Maryland");
states.Add("Virginia");
states.Add("California");
states.Add("Nevada");
states.Add("Ohio");

CompositeCollection cmpc = new CompositeCollection();
CollectionContainer cc1 = new CollectionContainer();
CollectionContainer cc2 = new CollectionContainer();

cc1.Collection = cities;
cc2.Collection = states;

cmpc.Add(cc1);
cmpc.Add(cc2);

list.ItemsSource = cmpc;

foreach(var itm in cmpc)
{
    // itm is CollectionContainer and there are only two itm’s
    // I need the strings
}

虽然列表在 GUI 上显示了正确的数据,但

我需要此数据(不引用列表框),但我没有得到它

Consider the code:

ObservableCollection<string> cities = new ObservableCollection<string>();
ObservableCollection<string> states = new ObservableCollection<string>();

ListBox list;

cities.Add("Frederick");
cities.Add("Germantown");
cities.Add("Arlington");
cities.Add("Burbank");
cities.Add("Newton");
cities.Add("Watertown");
cities.Add("Pasadena");

states.Add("Maryland");
states.Add("Virginia");
states.Add("California");
states.Add("Nevada");
states.Add("Ohio");

CompositeCollection cmpc = new CompositeCollection();
CollectionContainer cc1 = new CollectionContainer();
CollectionContainer cc2 = new CollectionContainer();

cc1.Collection = cities;
cc2.Collection = states;

cmpc.Add(cc1);
cmpc.Add(cc2);

list.ItemsSource = cmpc;

foreach(var itm in cmpc)
{
    // itm is CollectionContainer and there are only two itm’s
    // I need the strings
}

While list shows the right data on the GUI

I need this data (without referring to the ListBox) and I am not getting it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆凄美了谁 2024-09-12 06:43:00

试试这个: foreach (var itm in cmpc.Cast().SelectMany(x => x.Collection.Cast()))

Try this: foreach (var itm in cmpc.Cast<CollectionContainer>().SelectMany(x => x.Collection.Cast<string>()))

冰雪之触 2024-09-12 06:43:00

ListBox 为其 ItemsSource 属性使用集合的默认视图,您也可以使用它:

  foreach (string itm in CollectionViewSource.GetDefaultView(cmpc))
  {
    Debug.Print(itm);
  }

您可以使用 ICollectionView 类进行排序或过滤 ItemsSource,但要小心,这不能与 CompositeCollection 一起正常工作,正如您可以看到这个问题:如何使用 CollectionView 功能处理 CompositeCollection?

The ListBox uses for its ItemsSource Property the default view of the collection, which you can use, too:

  foreach (string itm in CollectionViewSource.GetDefaultView(cmpc))
  {
    Debug.Print(itm);
  }

You can use the ICollectionView classes to sort or filter an ItemsSource, but be careful while this won't work properly with CompositeCollections, as you can see this question: How to handle a CompositeCollection with CollectionView features?

青衫负雪 2024-09-12 06:43:00

您应该从 cmpc 项目中提取数据并将其设置为列表数据源。ItemsSource 不会理解您需要将项目的内部项目设置为数据源
编辑

您可以使用此方法

List<string> GetData(CompositeCollection cmpc)
        {
            List<string> allStrings = new List<string>();
            foreach (var item in cmpc)
            {
                allStrings.AddRange(item.OfType<string>());
            }
            return allStrings;
        }

并设置数据源

list.ItemsSource = GetData(cmpc);

you should extract data from cmpc items and set them as data source as list.ItemsSource won't understand that u need to set inner items of items as a datasource
EDIT

You can use this method

List<string> GetData(CompositeCollection cmpc)
        {
            List<string> allStrings = new List<string>();
            foreach (var item in cmpc)
            {
                allStrings.AddRange(item.OfType<string>());
            }
            return allStrings;
        }

and set datasource

list.ItemsSource = GetData(cmpc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文