You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When inserting an item into an ObservableCollection that is bound to a ComboBox, which in turn Is Editable, the control selects the wrong index when the control is collapsed due du a click outside the control.
It works if the control is not editable, if an item is added after the selected index, if the control is collapsed by clicking the up chevron.
A variant of this issue is to use an ordinary list as a binding source
Steps to reproduce the bug
Create a project, add the following code to the MainWindow:
using Microsoft.UI.Xaml;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace App1;
public record Customer
{
public Customer(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; }
public string Name { get; }
}
public sealed partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public MainWindow()
{
this.InitializeComponent();
AddCustomer();
AddCustomer();
AddCustomer();
SelectedCustomer = 2;
}
public ObservableCollection<Customer> Customers { get; } = new();
List<Customer> _customersArray = new();
public List<Customer> CustomersArray
{
get => _customersArray;
set
{
if (_customersArray != value)
{
_customersArray = value;
PropertyChanged?.Invoke(this, new(nameof(CustomersArray)));
}
}
}
int? _selectedCustomer;
public int? SelectedCustomer
{
get => _selectedCustomer;
set
{
if (_selectedCustomer != value)
{
_selectedCustomer = value;
PropertyChanged?.Invoke(this, new(nameof(SelectedCustomer)));
}
}
}
void Add_Click(object sender, RoutedEventArgs e)
{
AddCustomer();
}
void Insert_Click(object sender, RoutedEventArgs e)
{
InsertCustomer();
}
int _id = 0;
void AddCustomer()
{
_id++;
Customers.Add(new(_id, $"Customer {_id}"));
}
void InsertCustomer()
{
_id++;
Customers.Insert(2, new(_id, $"Customer {_id}"));
}
void UpdateCustomersArray()
{
var sel = CB.SelectedValue;
CB.SelectedValue = null;
CustomersArray = Customers.ToList();
CB.SelectedValue = sel;
}
}
A similar issue
Expected behavior
The Control should maintain the selected value and refresh accordingly.
It seems to me that it maintains the selected index, which does not work in this case.
Screenshots
Please see the screen recording. The drop down part of the control is missing, but you can still see the error at the end! screen-recording-2024-05-23-08_07.webm
NuGet package version
WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000
Windows version
Windows 11 (22H2): Build 22621
Additional context
No response
The text was updated successfully, but these errors were encountered:
I can reproduce the issue. As you said, it appears that it is maintaining the selected index instead of the item. You get the correct behavior if you actually select an item from the popup. But you get the incorrect behavior if you dismiss the popup by clicking away.
Describe the bug
When inserting an item into an ObservableCollection that is bound to a ComboBox, which in turn Is Editable, the control selects the wrong index when the control is collapsed due du a click outside the control.
It works if the control is not editable, if an item is added after the selected index, if the control is collapsed by clicking the up chevron.
A variant of this issue is to use an ordinary list as a binding source
Steps to reproduce the bug
Create a project, add the following code to the MainWindow:
and for the code behind:
A similar issue
Expected behavior
The Control should maintain the selected value and refresh accordingly.
It seems to me that it maintains the selected index, which does not work in this case.
Screenshots
Please see the screen recording. The drop down part of the control is missing, but you can still see the error at the end!
screen-recording-2024-05-23-08_07.webm
NuGet package version
WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000
Windows version
Windows 11 (22H2): Build 22621
Additional context
No response
The text was updated successfully, but these errors were encountered: