microsoft / microsoft/microsoft-ui-xaml
ComboBox - Setting SelectedItem to an item within the ComboBox does not select the item
- 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:

ComboBox is properly populated:

Breakpoint shows SelectedItem property is null after selection:

### 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
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