How to synchronize ListCollectionView.CurrentItem and ListView.SelectedItem?
Asked Answered
B

1

5

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?

Bourbon answered 14/10, 2015 at 14:38 Comment(1)
Bind the the selected item of the control (ListView).Fixed
L
7

Set the IsSynchronizedWithCurrentItem property:

<ListView ItemsSource="{Binding Pacientes}" IsSynchronizedWithCurrentItem="True"/>
Limicolous answered 15/10, 2015 at 7:7 Comment(1)
Wow, that simple... I've read a lot about all this stuff, and sure I've read about IsSync..., but I haven't figured out that is the reason it is supposed to be used... My bad. Thanks a lot!Bourbon

© 2022 - 2024 — McMap. All rights reserved.