按住鼠标左键时从多选集合中获取最后一项

Get last item from multiselection collection when keep holding pressed mouse left button

我有一个基于 ListView 的 WPF MVVM 应用程序。我在以下步骤中描述了一个小难题:

  1. 我用ctrl + 鼠标左键多选了多条记录。始终显示最后一条记录的信息。
  2. 我在最后一条记录上按了 ctrl + 鼠标左键并一直按住鼠标键。显示第一条记录的信息。
  3. 松开鼠标,显示最后一条记录的信息

我发现 "Since I have Multiple selection, the SelectedItem will always be the first selected in my selection."

的信息

我可以覆盖行为并立即将 Selecteditem 设置为最后一个元素吗?如果是的话,你能给我一些如何实现这一目标的建议吗?我会很感激的。

XAML:

 <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChanged}" CommandParameter="{Binding ElementName=EmployeeListView, Path=SelectedItems}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseLeftButtonUp">
                    <i:InvokeCommandAction Command="{Binding ClickCommand}" CommandParameter="{Binding ElementName=EmployeeListView, Path=SelectedItems}"/>
                </i:EventTrigger>

点击方式:

public void ClickMethod(object parameter)
        {
            IList selection = (IList)parameter;

            if (Keyboard.IsKeyDown(Key.LeftCtrl))
        {
            if (SelectedEmployee == null)
            {
                IndexesOfSelectedEmployees.Clear();
                CountOfSelectedEmployees = 0;
                DynamicSearchEmployeeName = String.Empty;
                DynamicSearchEmployeeID = String.Empty;
                DynamicSearchEmployeeSalary = String.Empty;
                DynamicSearchEmployeeDesigner = String.Empty;
                DynamicSearchEmployeeEmailID = String.Empty;
            }

            if (SelectedEmployee != null)
            {
                foreach (Employee item in Employees)
                {
                    if (item.IsSelected == true)
                    {
                        IndexesOfSelectedEmployees.Add(Employees.IndexOf(item));
                    }
                    if (item.IsSelected == false)
                    {
                        IndexesOfSelectedEmployees.Remove(Employees.IndexOf(item));
                    }
                }

                SelectedEmployee = Employees[IndexesOfSelectedEmployees.Last()];

                foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees)
                {
                    foreach (Employee itemEmployees in Employees)
                    {
                        if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
                        {
                            itemEmployees.IsSelected = true;
                        }
                    }
                }
                CountOfSelectedEmployees = selection.Count;
            }
        }
        else if (!IndexesOfSelectedEmployees.Any())
        {
            IndexesOfSelectedEmployees.Clear();
            IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
            CountOfSelectedEmployees = selection.Count;
        }
        else if (IndexesOfSelectedEmployees.Any())
        {
            if (IndexesOfSelectedEmployees.Count == 1 && IndexesOfSelectedEmployees.Contains((int)SelectedIndex))
            {
                //IndexesOfSelectedEmployees.Add(Employees.IndexOf(SelectedEmployee));
                SelectedEmployee = null;
                CountOfSelectedEmployees = 0;
                IndexesOfSelectedEmployees.Clear();
                DynamicSearchEmployeeName = String.Empty;
                DynamicSearchEmployeeID = String.Empty;
                DynamicSearchEmployeeSalary = String.Empty;
                DynamicSearchEmployeeDesigner = String.Empty;
                DynamicSearchEmployeeEmailID = String.Empty;
            }
            else if (IndexesOfSelectedEmployees.Count == 1 && !IndexesOfSelectedEmployees.Contains((int)SelectedIndex))
            {
                IndexesOfSelectedEmployees.Clear();
                IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
                CountOfSelectedEmployees = 1; 
            }
            else if (IndexesOfSelectedEmployees.Count != 1)
            {
                IndexesOfSelectedEmployees.Clear();
                IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
                SelectedEmployee = Employees[IndexesOfSelectedEmployees.Last()];
                foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees)
                {
                    foreach (Employee itemEmployees in Employees)
                    {
                        if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
                        {
                            itemEmployees.IsSelected = true;
                        }
                        if (itemIndexesOfSelectedEmployees != Employees.IndexOf(itemEmployees))
                        {
                            itemEmployees.IsSelected = false;
                        }
                    }
                }
                CountOfSelectedEmployees = selection.Count;
            }
        }


    }

SelectionChanged 方法:

    public void OnSelectionChanged(object parameter)
            {
                IList selection = (IList)parameter;

                if ((Keyboard.IsKeyDown(Key.LeftCtrl) && (Keyboard.IsKeyDown(Key.A))) == true || (Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.I

sKeyDown(Key.Up)) || (Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.IsKeyDown(Key.Down)))
            {
                CountOfSelectedEmployees = selection.Count;

                foreach (Employee item in selection)
                {
                    IndexesOfSelectedEmployees.Add(Employees.IndexOf(item));
                }                
                foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees)
                {
                    foreach (Employee itemEmployees in Employees)
                    {
                        if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
                        {
                            itemEmployees.IsSelected = true;
                        }
                    }
                }
            }
        }

我找到了解决办法。您必须恢复 IndexesOfSelectedEmployees 集合并且它有效。