我有一个基于ListView的WPF MVVM应用程序。我在以下步骤中描述了一个小难题:
我发现以下信息:“由于有多项选择,因此SelectedItem将始终是我选择中的第一个选择项。”
我可以覆盖行为并将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;
}
}
}
}
}
答案 0 :(得分:0)
我找到了解决方案。您必须还原IndexesOfSelectedEmployees集合,它才能工作。