How to bind multiple selection of listview to viewmodel?

后端 未结 9 2165
刺人心
刺人心 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:57

    If you are using System.Windows.Interactivity and Microsoft.Expression.Interactions already, here is a workaround without any other code/behaviour to mess around. If you need these, it can be download from here

    This workaround make use of interactivity event trigger and interactions set property mechanism in above assemblies.

    Additional namespace declaration in XAML

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    

    XAML:

    
      
        
          
        
      
    
    

    View Model:

    public class ModelListViewModel
    {
      public ObservableCollection ModelList { get; set; }
      public ObservableCollection SelectedModels { get; set; }
    
      public ModelListViewModel() {
        ModelList = new ObservableCollection();
        SelectedModels = new ObservableCollection();
      }
    
      public System.Collections.IList SelectedItems {
        get {
          return SelectedModels;
        }
        set {
          SelectedModels.Clear();
          foreach (Model model in value) {
            SelectedModels.Add(model);
          }
        }
      }
    }
    

    In example above, your ViewModel will pick up the selected items whenever the selection on ListView changed.

提交回复
热议问题