How to make the first item of a ListView to be selected as default at startup?

前端 未结 13 1043
甜味超标
甜味超标 2020-12-11 16:08

There is a ListView in my App and the ListView has a selector. I want to make the first item of this ListView to be selected as default at the very

13条回答
  •  醉酒成梦
    2020-12-11 16:46

    I have used data binding to solve this,

    using the following codebehind:

    private List _units;
    private Unit _selectedUnit;
    public Unit selectedUnit{
      get{
        if (_selectedUnit is null){
          return _units.Count > 0 ? _units[0] : null;
        }else{
          return _selectedUnit;
        }
      }
      set{
        if (_selectedUnit != value){
          _selectedUnit = value;
          OnPropertyChanged("selectedUnit");
        }
      }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName){
      var changed = PropertyChanged;
      if (changed != null){
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
    

    and XAML

    
    

提交回复
热议问题