Listbox First Item Selected in mvvm

喜夏-厌秋 提交于 2019-12-02 06:28:17

Do the following steps:

  1. Make sure that the collection Categorys is filled already. You might need to use AsycCTP, Asynchronous Programming with Async and Await or some other mechanism to first wait for the collection to be filled.

    The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.

  2. Implement INotifyPropertyChanged in ViewModel exposing the Property, CurrentCategory and raise the event of PropertyChanged from within the Setter of the Property.

    private Category _currentCategory = null;
    
    public Category CurrentCategory 
    {
        get { return _currentCategory; }
        set
        {
            if (_currentCategory != value)
            { 
                _currentCategory = value;
    
                // Update bindings
                RaisePropertyChanged("CurrentCategory");
            }
        }
    }
    

Now you can use the same piece of code:

CurrentCategory = Categorys[0];

Try using ICollectionView and IsSynchronizedWithCurrentItem. The CollectionView has all the functionality you need. For example MoveToFirst().

Xaml:

<ListBox ItemsSource="{Binding Categories}" 
                 DisplayMemberPath="Name" 
                 IsSynchronizedWithCurrentItem="True" />

ViewModel:

 public class ViewModel :INotifyPropertyChanged
        {
            private ObservableCollection<Category> _categories = new ObservableCollection<Category>();
            private Category _currentCategory;

            public ObservableCollection<Category> Categories
            {
                get { return _categories; }
                set { _categories = value; OnPropertyChanged("Categories");}
            }

            public Category CurrentCategory
            {
                get { return _currentCategory; }
                set { _currentCategory = value; OnPropertyChanged("CurrentCategory");}
            }

            public ICollectionView CategoriesView { get; private set; }

            public ViewModel()
            {
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat1"});
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat2"});
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat3"});

                CategoriesView = CollectionViewSource.GetDefaultView(Categories);
                CategoriesView.CurrentChanged += OnCategoriesChanged;
                CategoriesView.MoveCurrentToFirst();
            }

            private void OnCategoriesChanged(object sender, EventArgs e)
            {
                var selectedCategory = CategoriesView.CurrentItem as Category;
                if (selectedCategory == null) return;

                CurrentCategory = selectedCategory;
            }

            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public class Category
        {
            public Guid Id { get; set; }

            public string Name { get; set; }
        }

You Should Try This Way also.................

List c = new List

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