WPF - MVVM - ComboBox SelectedItem

前端 未结 4 646
春和景丽
春和景丽 2021-02-07 05:21

I have ViewModel(implemented INotifyPropertyChanged) in the background and class Category which has only one property of type string

4条回答
  •  悲哀的现实
    2021-02-07 05:55

    From my little example:

    Note: This is setting just a string (or a category from another list), but the basics should be same here:

    Basically this is done:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        (this.DataContext as ComboBoxSampleViewModel).SelectCategory("Categorie 4");
    }
    

    Here is my XAML:

    
        
        

    and in the ViewModel of the Window

    class ComboBoxSampleViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        public CategoryList List { get; set; }
    
        public ComboBoxSampleViewModel()
        {
            this.List = new CategoryList();
            NodeCategory = List.Selected;
        }
    
        private ComboBoxSampleItemViewModel nodeCategory;
        public ComboBoxSampleItemViewModel NodeCategory
        {
            get
            {
                return nodeCategory;
            }
            set
            {
                nodeCategory = value;
                NotifyPropertyChanged("NodeCategory");
            }
        }
    
        internal void SelectCategory(string p)
        {
            this.List.SelectByName(p);
            this.NodeCategory = this.List.Selected;
        }
    }
    

    With the help of this little class:

    public class CategoryList
    {
        public ObservableCollection Categories { get; set; }
        public ComboBoxSampleItemViewModel Selected { get; set; }
        public CategoryList()
        {
            Categories = new ObservableCollection();
    
            var cat1 = new ComboBoxSampleItemViewModel() { Name = "Categorie 1" };
            var cat2 = new ComboBoxSampleItemViewModel() { Name = "Categorie 2" };
            var cat3 = new ComboBoxSampleItemViewModel() { Name = "Categorie 3" };
            var cat4 = new ComboBoxSampleItemViewModel() { Name = "Categorie 4" };
    
            Categories.Add(cat1);
            Categories.Add(cat2);
            Categories.Add(cat3);
            Categories.Add(cat4);
    
            this.Selected = cat3;
        }
    
        internal void SelectByName(string p)
        {
            this.Selected = this.Categories.Where(s => s.Name.Equals(p)).FirstOrDefault();
        }
    }
    

    And this Item ViewModel

    public class ComboBoxSampleItemViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        private string name;
    
        public string Name 
        { 
            get
            {
                return name;
            }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
    

提交回复
热议问题