microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.34k stars 677 forks source link

Combobox SelectedValue property not working #4035

Open ssaran046 opened 3 years ago

ssaran046 commented 3 years ago

Describe the bug

I'm facing the same issue already reported https://github.com/microsoft/microsoft-ui-xaml/issues/3875.

Additionally, The SelectedValue property binding is not working for the framework type. So the Combobox item is not selected.

Sample: ComboBoxwithEnumvalues.zip

Steps to reproduce the bug

  1. Run the sample
  2. Now, the Combobox item is not selected

Expected behavior

The Combobox item needs to be selected.

Screenshots

image

Version Info

NuGet package version: [Microsoft.WinUI 3.0.0-preview3.201113.0]

Windows app type: UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

huoyaoyuan commented 3 years ago

Also #3339

kalin-todorov commented 3 years ago

Any updates on this issue?

shelllet commented 3 years ago

selectvalue can work by click button, except first ui loaded.

i'm crazy!

tuggernuts commented 2 years ago

Is anyone able to get SelectedValue to work with any object type? I can get it to work only if I hot-edit the SelectedValuePath, which is not ideal.

noaksx3 commented 2 years ago

I am also experiencing this same issue. In my case I am using the Microsoft Toolkit MVVM, and attempting to set the ComboBox.SelectedValue property to a property on the view model, and it's not selecting it from the list, even though the value of the view model property is the same as the value in the list.

Eltypo commented 2 years ago

Any updates?

AFriendlyGuy commented 2 years ago

I have the same problem as @tuggernuts:

<ComboBox SelectedValue="{x:Bind ViewModel.AlgorithmId, Mode=TwoWay}" ItemsSource="{x:Bind AvailableAlgorithms}"
                  SelectedValuePath="Id" DisplayMemberPath="Name" 
                  PlaceholderText="nothing selected" />

shows the placeholder text after initialization even though ComboBox.SelectedValue has a value which is also the value of the Id-property of one element in ComboBox.ItemsSource. If I hot-edit ComboBox.SelectedValuePath from "Id" to something else and back to "Id", the placeholder text disappears and the value of the Name-property of the correct item appears.

I suppose this is slightly different than the problem described in this issue. But it does not seem worth the effort to create a new issue, when this issues is open for more than 1.5 years.

AMArostegui commented 1 year ago

Same here. After playing with SelectedValue and SelectedValuePath I've been unable to get the data updated when the comobox changes.

bawkee commented 10 months ago

This is still a bug in WinUI3... is there at least a workaround?

Khiro95 commented 5 months ago

I wish to understand why some important (and sometimes easy) issues are being ignored for long time...

Anyway, I think I found the cause the of the bug after debugging for some minutes.

This bug is caused by two issues in ComboBox:

[!note] This behavior is observed when using x:Bind.

Workaround

// In constructor of Page/Window/UserControl/etc...
comboBox.RegisterPropertyChangedCallback(ComboBox.ItemsSourceProperty, OnItemsSourceChanged);

// This will be called whenever ItemsSource has changed
private void OnItemsSourceChanged(DependencyObject sender, DependencyProperty dp)
{
    if (comboBox.ItemsSource is not null)
    {
        comboBox.SelectedValuePath = null;
        comboBox.SelectedValuePath = "<the-path-here>";
    }
}