Find several template generated ComboBox items by property value - WinUI 3
I have a ComboBox bound to a ViewModel. Is there a shortcut to get the item's index by the property which is not displayed, i assume there isn't?
My target:
I have to find an item by property value to select the index.
If the property's value is not found, keep searching for an other item's property to select it.
The items in the ComboBox are changing that's why i want to find the items dynamically.
(In C# I think i could use maybe the find method with "x => x.a == xb" i guess, I'm not shure?!
I think i could also iterate it here in an other method or an anonymous function?!)
I'm iterating the ComboBox and getting the item and index like so, which is trigger by a ListView's Click event:
for (size_t i = 0; i < s_selectFunction->cmbFunction().Items().Size(); i++)
{
auto itemName = s_selectFunction->cmbFunction().Items().GetAt(i).as<App1::Function>().Name();
//auto item = s_selectFunction->cmbFunction().ItemContainerGenerator().ContainerFromItem(s_selectFunction->cmbFunction().Items().GetAt(i));
//auto itemName = item.as<App1::Function>();
if (keyCommand == winrt::to_string(itemName))
{
s_selectFunction->cmbFunction().SelectedIndex(i);
found = true;
break;
}
}
if (found == false)
{
// This won't work because the index is changing in an other scenario.
s_selectFunction->cmbFunction().SelectedIndex(21);
}
I also have a few things in mind which doesn't help I guess:
ItemsControl.ContainerFromItem(Object) Method