VShetty
VShetty

Reputation: 69

How to auto update the selected row in listview after save sucess in wpf mvvm

I'm using a listview in WPF like this:

<ListView x:Name="lv" ItemsSource="{Binding Path=xyz}" SelectedItem="{Binding SelectedRow}" >
    <ListView.View>
        <GridView>
            <GridViewColumn  Width="50"  DisplayMemberBinding="{Binding }" />
            <GridViewColumn   Width="140" DisplayMemberBinding="{Binding }" />
            <GridViewColumn Width="140" DisplayMemberBinding="{Binding }" />
            <GridViewColumn  Width="120" DisplayMemberBinding="{Binding }" />
        </GridView>
    </ListView.View>
</ListView>

On click of save in the form the selected row should jump to the next row.Like currentselectedrow+1.

How do I achieve this in WPF MVVM?

Upvotes: 0

Views: 235

Answers (1)

Frerk Morrin
Frerk Morrin

Reputation: 729

At first, I would suggest using SelectedIndex instead of SelectedItem.

<ListView x:Name="lv" ItemsSource="{Binding Path=xyz}" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" />

Then in your Save Method in your Viewmodel, you can simply increase the SelectedIndex by 1 (Remember to Check if the Index is not the last)

private void Save()
{
    // Your Save Logic...

    if (SelectedIndex + 1 < xyz.Count)
        SelectedIndex++;
}

if you want to keep that SelectedItem, than you can do something similar like

private void Save()
{
    //Your Save Logic...

    var index = xyz.IndexOf(this.SelectedItem);
    if (index - 1 < xyz.Count)
        SelectedItem = xyz.ElementAt(index + 1);
}

Upvotes: 3

Related Questions