microsoft / microsoft/microsoft-ui-xaml

ComboBox - Setting SelectedItem to an item within the ComboBox does not select the item

Open
#10,023 0 comments 0 reactions 0 assignees View on GitHub
area-ComboBox team-Controls
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

If you try to use the ComboBox SelectedItem to select an entry within the ComboBox, the entry is not being selected.

NOTE that this use to work, but I am not sure which Visual Studio and/or WinAppSDK update changed this functionality.

I use to be able to load a ComboBox with strings, then select an item by setting SelectedItem = "myString";

### Steps to reproduce the bug

1. Load a ComboBox with a series of strings.
2. Use the ComboBox's SelectedItem property to select a string in the middle of the list.

Issue: instead of displaying the string the ComboBox's display remains blank.

### Expected behavior

You should be able to display an item within a ComboBox by setting its SelectedItem property in the same manner as setting its SelectedIndex property.

### Screenshots

The code loads a ComboBox and tries to select an item by setting SelectedItem property.

However, the item is not selected and ComboBox is blank.

Blank ComboBox at startup:
![image](https://github.com/user-attachments/assets/ce0890f6-6f8d-4b22-88f1-80807ac21b06)

ComboBox is properly populated:
![image](https://github.com/user-attachments/assets/11d82b9e-25ab-4262-bcb4-28a6974d8c51)

Breakpoint shows SelectedItem property is null after selection:
![image](https://github.com/user-attachments/assets/a104b1e7-b154-4297-ab0d-2d3005f7a06b)

### NuGet package version

Windows App SDK 1.6.0: 1.6.240829007

### Packaging type

Packaged (MSIX)

### Windows version

_No response_

### IDE

Visual Studio 2022

### Additional context

MainWindow.xaml



MainWindow.xaml.cs
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();

AddComboBoxMembers();
}

private void AddComboBoxMembers()
{
int iIdx;

string sItem;

ComboBoxItem comboBoxItem;

for (iIdx = 0; iIdx < 10; ++iIdx)
{
sItem = "Item" + iIdx.ToString();

comboBoxItem = new ComboBoxItem
{
Content = sItem,
};

ComboBoxMain.Items.Add(comboBoxItem);
}

sItem = "Item5";

ComboBoxMain.SelectedItem = sItem;
}

}

Contributor guide

Open the contributing guide

Research direction

Start with MainWindow.xaml.cs and the AddComboBoxMembers method, then reproduce the issue using the ComboBox populated with ComboBoxItem objects and the SelectedItem assignment shown. Compare the populated item values with the assigned selection and verify the ComboBox displays Item5 and retains the expected SelectedItem value.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.