WPF Sorting an ObservableCollection de-selects a ComboBox

前端 未结 2 1538
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 12:43

I have a ComboBox where a user can select what JobType they are working on. The ComboBox has a list of AllJobTypes. The problem stems from when a user adds a new JobType,

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 13:12

    Here's a version using ItemsSource/SelectedItem. Note that you can add a new item to the list and sort it without losing the currently selected item in the view.

    The window

    
        
            
                
                
            
            
            
        
    
    

    The code

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
    
    public class Notifier : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify([CallerMemberName]string property = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }
    }
    
    public class ViewModel : Notifier
    {
        private JobType _selectedJobType;
        private string _newJobType;
    
        public JobTypeList JobTypes { get; private set; } 
        public JobType SelectedJobType { get => _selectedJobType; set { _selectedJobType = value; Notify(); } }
        public string NewJobType { get => _newJobType; set { _newJobType = value; Notify(); AddNewJobType(value); } }
        public ViewModel()
        {
            JobTypes = new JobTypeList();
            JobTypes.Add(new JobType { Name = "Butcher" });
            JobTypes.Add(new JobType { Name = "Baker" });
            JobTypes.Add(new JobType { Name = "LED maker" });
        }
    
        private void AddNewJobType(string name)
        {
            if(JobTypes.Any(x => x.Name == name)) return;
            JobTypes.Add(new JobType { Name = name });
            JobTypes.SortJobTypes();
        }
    }
    public class JobType : Notifier
    {
        private string _name;
    
        public string Name { get => _name; set { _name = value; Notify(); } }
    }
    

    Using your JobTypesList

    public class JobTypeList : ObservableCollection
    {
    
        public void SortJobTypes()
        {
            var sortableList = new List(this);
            sortableList.Sort((x, y) => x.Name.CompareTo(y.Name));
            //this works but it creates a bug in the select for JobTypes
            for(int i = 0; i < sortableList.Count; i++)
            {
                this.Move(this.IndexOf(sortableList[i]), i);
            }
        }
    }
    

提交回复
热议问题