使用Multiple SelectionMode基于ListView选择执行命令

时间:2016-05-02 13:26:44

标签: wpf mvvm mvvm-light

如何使用ListView的Multiple SelectionMode处理命令执行? ListView ItemsSource绑定到ObservableCollection(EF表),并且我将选择限制设置为2:

<ListView ItemsSource="{Binding EmployeeViewM.MainActivity}"   
                       SelectionMode="Multiple" 
                       IsSynchronizedWithCurrentItem="True" 
                       HorizontalAlignment="Right" 
                       BorderBrush="{x:Null}" 
                       Background="{x:Null}"
                       VirtualizingPanel.IsVirtualizing="True" 
                       VirtualizingPanel.IsContainerVirtualizable="True"                            
                       VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                       VirtualizingPanel.ScrollUnit="Item" 
                       VirtualizingPanel.VirtualizationMode="Standard">

<I:Interaction.Behaviors>
   <limit:LimitSelectionBehavior Limit="2"/>
</I:Interaction.Behaviors>

我想根据所选项目的数量执行命令(RelayCommand)。类似的东西:

    If SelectItems.Count = 1 Then
        'Get first item selected
        'Save to the database to table 1
        'Start Timer_01
    ElseIf SelectItems.Count = 2 Then
        'Get last selected item
        'Save to the database to table 2
        'Start Timer_02
    End If

如何在此处理多项选择? 感谢您的任何建议

1 个答案:

答案 0 :(得分:0)

获取列表视图的选择更改事件,并遍历列表以找出选择的项目。检查下面的示例代码!

public void Selection_Changed(object sender, EventArguments arg)
{
  List<DataClass> selectedItems = new List<DataClass>();

  foreach(DataClass item in LISTVIEWNAME.SelectedItems)
  {
    selectedItems.Add(item);
  }
}

现在 selectedItems 将在列表视图中显示所选列表项的列表。希望你明白了!