How to bind multiple selection of listview to viewmodel?

后端 未结 9 2163
刺人心
刺人心 2020-12-13 09:14

I am implementing a listview, and a button next to it. I have to be able that when i select multiple items in a listview, and then click on a button, then the selected items

9条回答
  •  误落风尘
    2020-12-13 09:39

    What you can do is you can handle the Button_Click(...) in your code-behind. Then in that code-behind method you can create a List of selected items by iterating over the selected items of the listView.

    Since it is allowed to access the ViewModel from the View you can now call a method on your ViewModel and pass the list of selected items as a parameter.

    I'm not sure if this would also work with Bindings only, however it is not bad practice to use code-behind as well.

    Example Code:

    public void Button_Click(object sender, EventArguments arg)
    {
      List mySelectedItems = new List();
    
      foreach(ListViewItem item in myListView.SelectedItems)
      {
        mySelectedItems.Add(item);
      }
    
      ViewModel.SomeMethod(mySelectedItems);
    }
    

    EDIT

    Here is a minimalist example, XAML:

    
    
    
    
    
    

    CODE-BEHIND:

    public void Button_Click(object sender, EventArguments arg)
        {
          List mySelectedItems = new List();
    
          foreach(Car item in myListView.SelectedItems)
          {
            mySelectedItems.Add(item);
          }
    
          ViewModel.SomeMethod(mySelectedItems);
        }
    

提交回复
热议问题