How to preserve TwoWay binding of CurrentItem when databinding to CollectionViewSource in ComboBox

前端 未结 4 1831
粉色の甜心
粉色の甜心 2021-01-04 03:07

Lets say we got a simple VM class

public class PersonViewModel : Observable
    {
        private Person m_Person= new Person(\"Mike\", \"Smith\");

                 


        
4条回答
  •  失恋的感觉
    2021-01-04 03:53

    Have you considered using a CollectionView and set IsSynchronizedWithCurrentItem on the combobox?

    That is what I would do - instead of having your CurrentPerson property you have the selected person on your collectionView.CurrentItem and the combobox following currentitem on the collectionview.

    I Have used collectionview with sorting and grouping without problems - and you get a nice decoupling from ui with it.

    I would move the collectionview to code and bind to it there

    public ICollectionView AvailablePersonsView {get;private set;}

    in ctor:

    AvailablePersonsView = CollectionViewSource.GetDefaultView(AvailablePersons)

提交回复
热议问题