avalonia ComboBox 绑定后无法选中项的问题

有如下代码:

                              <ComboBox ItemsSource="{Binding Apps}"
                                      SelectedItem="{Binding SelectedApp}"
                                      SelectionChanged="CbxApp_OnSelectionChanged"
                                      Width="120" PlaceholderText="请选择" />
                            <ComboBox ItemsSource="{Binding Envs}"
                                      SelectedItem="{Binding SelectedEnv}" />

VM:

    public IList<String> Apps { get; set; } = new List<string>();
    public IList<String> Envs { get; set; } = new List<String>();
[Reactive] public String SelectedApp { get; set; }
[Reactive] public String SelectedEnv { get; set; }

Codebehind:

    private void CbxApp_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        var vm = this.DataContext as HttpInvokeHomeViewModel;
        if (vm == null)
        {
            return;
        }
        
        vm.BindEnv();
    }

问题:

绑定Envs 后,UI层 可以展开下拉,但是无法选中,

 

解决方案:

给ItemSource 设置为 ObservableCollection<String> 可解

public ObservableCollection<String> Apps { get; set; } = new ObservableCollection<string>();
    public ObservableCollection<String> Envs { get; set; } = new ObservableCollection<String>();

 

posted @ 2025-08-04 11:10  我的用户名  阅读(75)  评论(0)    收藏  举报