Home ยป The peculiar behavior of comboboxes in PowerApps

The peculiar behavior of comboboxes in PowerApps

  • by
comboboxes-defaultselecteditems
Tags:
Want to do a random act of kindness? Share this post.

No-code and low-code development platforms are the hype of 2020. Luckily, I had the honor of working on a business application for one of our clients. Since the whole sector is entrenched in the Microsoft ecosystem, PowerApps was a logical choice. It’s a great tool, but sometimes behaves in unexpected ways. Here’s a problem I lost hours with, figuring it out.

To explain the issue, I made this quick demonstration app. It has a Database icon that loads data into a collection. It has a gallery that lists the data in the collection. It has a combobox where users can choose an item from the collection and gets its DefaultSelectedItems from the selected item in the gallery, through a context variable. Finally, two labels to show you the problematic behavior of the combobox.

This is the collection, which is created at the OnSelect of the icon.

ClearCollect(
    TestCollection,
    Table(
        {
            fruit: "apples",
            count: 5
        },
        {
            fruit: "pears",
            count: 3
        },
        {
            fruit: "cherries",
            count: 2
        }
    )
);
UpdateContext({selectedFruit: ""})

The Items of the gallery are filled with the collection TestCollection. Once someone selects an item from the fallery, it preloads the combobox with the selected item, by loading the selectedFruit variable with its own value.

Select(Parent);
UpdateContext({selectedFruit: ThisItem.fruit})

The selectedFruit variable gets picked up by the combobox through its DefaultSelectedItems property.

[selectedfruit]

So, let’s run the app and see what happens.

Ass you can see, if I select the “apples” from the gallery, it sets the combobox’ default value. However, it only shows the “apples” in the second label, but not in the first one. But, if I select the “apples” manually in the combobox, it is shown in the first label. How is this possible?

The first label has the following Text property:

"V1: The fruit I selected in the combobox is: " & ComboBox1.Selected.fruit

And the second label has the following Text property:

"V2: The fruit I selected in the combobox is: " & If(IsBlank(ComboBox1.Selected.fruit),selectedfruit,ComboBox1.Selected.fruit)

This means that if you set the DefaultSelectedItems through a variable, the default selected item is actually not set, it only shows the label inside the combobox. It’s even crazier, because although nothing gets selected, it does trigger an OnChange event.

Apparently, I am not the only one who is flabbergasted that a property DefaultSelectedItems actually does not select anything: “So it’s expected that when the SelectedItems collection isn’t empty, it will have those entries selected as default.

Currently (february 2020) the only way around it is to always check if the selected items is blank, and fall back to the variable when it is.

Want to do a random act of kindness? Share this post.