microsoft-ui-xaml: The ComboBox doesn't select value if it's enum

Describe the bug Hi, the binding to the ComboBox with SelectedValuePath doesn’t work when I’ve placed the enum to SelectedValue. Please take a look at the following code.

Steps to reproduce the bug

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ComboBox SelectedValuePath="Value" 
                  DisplayMemberPath="Text"
                  ItemsSource="{x:Bind TestSource}"
                  SelectedValue="{x:Bind MyValue, Mode=TwoWay}"/>
    </StackPanel>
    public sealed partial class MainWindow : Window {
        public MainWindow() {
            TestSource = new DisplayItem[] {
                new DisplayItem(MyValues.Value1, "Value1"),
                new DisplayItem(MyValues.Value2, "Value2"),
            };
            MyValue = TestSource[0].Value;

            this.InitializeComponent();
        }

        public IList<DisplayItem> TestSource { get; }
        public MyValues MyValue { get; set; }
    }

    public enum MyValues { Value1, Value2 }

    public class DisplayItem {
        public DisplayItem(MyValues value, string text) {
            Value = value;
            Text = text;
        }

        public MyValues Value { get; }
        public string Text { get; }
    }

Expected behavior

Screenshots image

image

Version Info

NuGet package version:

[WinUI 3 - Windows App SDK 0.8: 0.8.0] If you’re seeing your issue in older previews of WinUI 3, please try this release.

Windows app type:

UWP Win32
Yes
Windows version Saw the problem?
Insider Build (xxxxx)
May 2021 Update (19043)
October 2020 Update (19042)
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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Dupe of #8919

How can you say this when this issue predates that one by two years? #8919 is a dupe of this issue.

This should not be closed.

This is a legitimate bug. As one person noted, enum is a value type. It has an ‘IsEqual’ and ‘Hash’ method, so there’s no reason an item can’t be found in an ItemsSource that provides the same type of value.

@bpulliam Can you reopen this or do I need to create a new issue?

@GeorgeKerwood Did you try my code? It’s doesn’t work. I know many ways to bind the SelectedItem. And your approach will work. But it has a lot of excessive code. The SelectedValuePath property exists only for one thing - to select a value instead of a whole item. This is a very simple and easy scenario when the ComboBox does “work” instead of you. In this case, you no need to write a code that syncs a real value in the viewmodel\model and the SelectedItem in the ComboBox. It’s a basic feature.