最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using enum as ComboBox value in WinUI3 doesn't work - Stack Overflow

programmeradmin3浏览0评论

Consider this XAML:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Text="Theme:" VerticalAlignment="Center" Margin="10 0" />
    <ComboBox Name="themeCB"
        ItemsSource="{x:Bind Themes}"
        SelectedValue="{x:Bind SelectedTheme}"
        DisplayMemberPath="DisplayText"
        SelectedValuePath="Value" />

    <TextBlock Text="Backdrop:" VerticalAlignment="Center" Margin="10 0" />
    <ComboBox Name="backdropCB"
                ItemsSource="{x:Bind Backdrops}"
                SelectedValue="{x:Bind SelectedBackdrop}"
                DisplayMemberPath="DisplayText"
                SelectedValuePath="Value" />
</StackPanel>

And the code behind:

public MainWindow()
{
    InitializeComponent();

    SelectedTheme = ElementTheme.Light;
    SelectedBackdrop = BackdropType.Mica;
}

public List<ComboBoxItem<ElementTheme>> Themes =>
[
    new ComboBoxItem<ElementTheme>("Light", ElementTheme.Light),
    new ComboBoxItem<ElementTheme>("Dark", ElementTheme.Dark),
    new ComboBoxItem<ElementTheme>("System", ElementTheme.Default),
];

public List<ComboBoxItem<BackdropType>> Backdrops =>
[
    new ComboBoxItem<BackdropType>("None", BackdropType.None),
    new ComboBoxItem<BackdropType>("Mica", BackdropType.Mica),
    new ComboBoxItem<BackdropType>("Mica Alt", BackdropType.MicaAlt),
    new ComboBoxItem<BackdropType>("Acrylic", BackdropType.Acrylic),
    new ComboBoxItem<BackdropType>("Acrylic Thin", BackdropType.AcrylicThin),
];

public ElementTheme SelectedTheme { get; set; }
public BackdropType SelectedBackdrop { get; set; }

The ComboBoxItem and BackdropType types are as follows:

public class ComboBoxItem<T>
{
    public string DisplayText { get; set; }
    public T Value { get; set; }

    public ComboBoxItem() { }
    public ComboBoxItem(string displayText, T value)
    {
        DisplayText = displayText;
        Value = value;
    }
}

public enum BackdropType
{
    None,
    Mica,
    MicaAlt,
    Acrylic,
    AcrylicThin
}

ElementTheme is from Microsoft.UI.Xaml.

The problem is, the Theme: ComboBox gets populated with the value set, which is "Light", but the Backdrop: ComboBox does NOT get populated with the value set, which is "Mica". Why is that? What am I missing?

P.S.: I am not looking for a solution to make it work - I can make it work in a number of ways. I am trying to understand why it works for one enum but doesn't for another.

发布评论

评论列表(0)

  1. 暂无评论