Page 1 of 1

C++ ItemControl

Posted: 10 Aug 2022, 03:58
by Sime0647
Hi, how can I add items to a ItemControl or any control derived from it in C++? I followed this C# WFP documentation: https://docs.microsoft.com/en-us/dotnet ... esktop-6.0
 Ptr<ItemsControl> pItemsControl = *new ItemsControl();
 Ptr<Binding> pBinding = *new Binding();
 Ptr<BaseComponent> pBoxed = Boxing::Box("FirstItem");
 pBinding->SetSource(pBoxed);
 pItemsControl->SetBinding(pItemsControl->ItemsSourceProperty, pBinding);
and also tried to only use the SetItemsSource() function
 Ptr<ItemsControl> pItemsControl = *new ItemsControl();
 Ptr<Binding> pBinding = *new Binding();
 Ptr<BaseComponent> pBoxed = Boxing::Box("FirstItem");
 pItemsControl->SetItemsSource(pBoxed);
But none of them worked...

Re: C++ ItemControl

Posted: 10 Aug 2022, 18:59
by sfernandez
An ItemsControl can be populated with items in 2 ways:
  • By adding items to its Items collection
    itemsControl->GetItems()->Add(Boxing::Box("First"));
    itemsControl->GetItems()->Add(Boxing::Box("Second"));
    itemsControl->GetItems()->Add(Boxing::Box("Third"));
  • By setting a collection as its ItemsSource
    Ptr<ObservableCollection<BaseComponent>> items = MakePtr<ObservableCollection<BaseComponent>>();
    items->Add(Boxing::Box("First"));
    items->Add(Boxing::Box("Second"));
    items->Add(Boxing::Box("Third"));
    itemsControl->SetItemsSource(items);
Hope this helps.