Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize ListCollectionView.CurrentItem and ListView.SelectedItem?

I have a ViewModel containing a ListCollectionView property, and a View containing a ListView control whose ItemsSource property is data bound to the ListCollectionView:

ViewModel:

    public ListCollectionView Pacientes { get; set; }

    public IRepositório<Paciente> RepositórioPacientes { get; set; }


    // CONSTRUTOR
    public MainViewModel()
    {
        RepositórioPacientes = new PacienteRepositorioFake();

        Pacientes = (ListCollectionView)CollectionViewSource.GetDefaultView(RepositórioPacientes.Items);
    }
}

View (heavily stripped down):

    <ListView ItemsSource="{Binding Pacientes}"/>
    <Border DataContext="{Binding Pacientes/}">
        <!-- some controls displaying properties of Paciente entity -->
    </Border>

Note the Binding Pacientes/ with a trailing slash, trying to bind to Pacientes.CurrentItem.

My intention here is to provide a Master/Detail view, with a ListView displaying all items, and a side panel displaying information from the Current/Selected Item.

The fact is: when I select an element in ListView, I would expect Pacientes.CurrentItem to be set, but apparently it is not!

So my question is: how can I set ListCollectionView.CurrentItem by selecting an item on a data bound ListBox?

like image 885
heltonbiker Avatar asked Oct 14 '15 14:10

heltonbiker


1 Answers

Set the IsSynchronizedWithCurrentItem property:

<ListView ItemsSource="{Binding Pacientes}" IsSynchronizedWithCurrentItem="True"/>
like image 187
Clemens Avatar answered Nov 08 '22 23:11

Clemens