I've implemented MVVM in WPF and have a ListView
as follows:
<ListView DockPanel.Dock="Top" Name="ListViewMain" ItemsSource="{Binding Items}">
... GridView in ListView.View ...
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding OnSelectionChangedCommand}"
CommandParameter="{Binding SelectedIndex,
ElementName=ListViewMain}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
Whenever I change selection by clicking on any item on the ListView
, OnSelectionChangedCommand
is called with correct SelectedIndex
.
But when I change SelectedIndex
programatically as follows:
ListViewMain.SelectedIndex = 0;
I get -1 in OnSelectionChangedCommand
. How do I get correct SelectedIndex
irrespective of selection change method?
Update
The answers in WPF Listview SelectionChanged event don't explain what'll happen when SelectedIndex
is set programmatically and how to define the view model's property which is bound do SelectedIndex
.