Find several template generated ComboBox items by property value - WinUI 3

youki 996 Reputation points
2023-01-22T21:29:08.1766667+00:00

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

How to: Find DataTemplate-Generated Elements

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
725 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes