-1

I want to get selected Item from ListView. My view is:

<ListView Name="StudentGrid" Grid.Row="1" Margin="1,1,1,1" ItemsSource="{Binding studentList}" SelectedItem="{Binding selectedItem}">

The ViewModel is:

public ObservableCollection<Student> selectedItem { get; set; }
private void DeleteStudent()
{
    ObservableCollection<Student> item = selectedItem;
    if(selectedItem != null)
    {
        int a = item.Count;
    }
}

I want to get index of the selected item. How can I do that?

Fardin
  • 66
  • 7

1 Answers1

1

The SelectedItem is an object in the bound collection, so it is of type Student and not ObservableCollection<Student> like the list itself. Furthermore, if you want the property to be bound two-way, meaning you can also change the index in the view model and the ListView will update the selected index accordingly, you have to implement INotifyPropertyChanged.

public class YourViewModel : INotifyPropertyChanged
{
   private Student _selectedItem;

   // ...other code.

   public Student SelectedItem
   {
      get => _selectedItem;
      set
      {
         if (_selectedItem == value)
            return;

         _selectedItem = value;
         OnPropertyChanged();
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}

To get the index, bind the SelectedIndex property, to a property of type int in your view model.

private int _selectedIndex;

public int SelectedIndex
{
   get => _selectedIndex;
   set
   {
      if (_selectedIndex == value)
         return;

      _selectedIndex = value;
      OnPropertyChanged();
   }
}
<ListView Name="StudentGrid" Grid.Row="1" Margin="1,1,1,1" ItemsSource="{Binding studentList}" SelectedIndex="{Binding SelectedIndex}"/>

You could also bind both or get the student by index from your list.

var student = studentList[SelectedIndex];
thatguy
  • 21,059
  • 6
  • 30
  • 40
  • Thanks. How can I detect selectedItem (for deleting from database)? –  Mar 17 '22 at 07:35
  • 1
    @Fardin What do you mean by _detect selectedItem_? The bound `SelectedItem` is a `Student` object from your list, the same for getting a `Student` by index. – thatguy Mar 17 '22 at 07:46
  • Ok, If I wanted to delete multiple selected items, do I need to use the `IList` property type and `foreach` loop for deleting items? –  Mar 17 '22 at 08:01
  • 1
    @Fardin That is more difficult. You have to set the [`SelectionMode`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.selectionmode?view=windowsdesktop-6.0) to `Multiple` or `Extended`. Then the selected items are stored in the [`SelectedItems`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.listbox.selecteditems?view=windowsdesktop-6.0) property, but this property is **not bindable**. For workarounds see [How to bind multiple selection of listview to viewmodel?](https://stackoverflow.com/q/5741161/6181599) – thatguy Mar 17 '22 at 08:11