Universal app - Loading combobox' ItemsSource async gives weird behaviour

穿精又带淫゛_ 提交于 2019-12-05 06:11:47

Found a workaround cause previous solutions didn't solve my problem.

Just add a pause between binding and selecting an item or index of your combobox.

Code below :

myCombobox.ItemsSource = myList;
await Task.Delay(100);
myCombobox.SelectedIndex = 12;

Hope this helps !

I checked it out and I can't see any problem with your code, so I guess it is a bug in the ComboBox.

Understanding the problem and finding a true fix may take you some time, so I'd suggest you use some workaround that works for you. I tried the following and it seemed to work:

  1. Change the Items property in the VM to be of type ObservableCollection<string>
  2. Initialize the property/field in the VM's constructor to an empty collection.
  3. When loading the items, just fill the collection (add items to it using the Add() method) instead of replacing it.

Edit: example of how I fill tested it.

public class VM : INotifyPropertyChanged
{
    private ObservableCollection<string> _items;
    public ObservableCollection<string> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            _selectedItem = _items.FirstOrDefault();
            RaisePropertyChanged("Items");
            RaisePropertyChanged("SelectedItem");
        }
    }

    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            RaisePropertyChanged("SelectedItem");
        }
    }


    public VM()
    {
        this._items = new ObservableCollection<string>();
    }

    public async Task LoadDataAsync()
    {
        var items = new List<string>() {
            "1",
            "b",
            "c",
            "d",
            "e",
            "f",
            "f",
            "f",
            "f",
            "f",
            "f",
        };

        foreach (var i in items) {
            this._items.Add(i);
        }
        this.SelectedItem = items.FirstOrDefault();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

This works fine for me.

Not only asynchronously - if you put _vm.Items = new List... in OnLoaded event, instead of await _vm.LoadDataAsync(); - you will get the same issue.

Seems that the issue won't occur once you set your Items before setting the DataContext.

The other thing is that the issue won't appear (as I've tried) if you don't set Selected item from code:

public ObservableCollection<string> Items
{
    get { return _items; }
    set
    {
        _items = value;
    //    _selectedItem = _items.FirstOrDefault();
        RaisePropertyChanged("Items");
     //   RaisePropertyChanged("SelectedItem");
    }
}

As for now I've no idea why this happens.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!