[英]Select and Delete multiple items in a ListBox and Collection at the same time?
我正在使用 xaml 和 wpf。 我有一个由集合填充的列表框中的待办事项列表。 我目前能够一次删除列表中的一项。 我将如何编辑它以便能够 select 并一次删除多个?
您可以使用 SelectedItems ListBox 属性并将其作为命令参数传递。 然后在 ViewModel 中,您将能够获取每个选定的项目。
记得设置
SelectionMode="Extended" //or Multiple
在您的列表框中。
示例:
这是我绑定到命令的按钮:
<Button Content="⇅"
ToolTip="Use ctr+click or shift+click to select more than one item"
Command="{Binding SwapVerticallyCommand}"
CommandParameter="{Binding ElementName=RightListBox, Path=SelectedItems}" />
如您所见,我将SelectedItems作为命令 arguments 传递。 然后在 VM 中执行此方法(类似于您的 RemoveTodoItem):
private void SwapVertically(IList obj)
{
if ( obj[0] is Slot first && obj[1] is Slot second )
{
(first.Target, second.Target) = (second.Target, first.Target); //swap places
}
}
IList作为参数传递,您可以从中获取值。 因此,如果您的命令获取 obj 然后将其转换为 IList,但更好的解决方案是定义从 View 传递给命令的 object 的类型 - 使用 IList 而不是 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.