我有两个 ListView,第一个是启用 MultipleSelection 的 ListView。当用户从第一个 ListView 中单击一个项目时,它会被添加到第二个 ListView 中,当他从第一个 listView 中取消选择一个项目时,它会从第二个 ListView 中删除。
我现在的问题是我希望用户能够从第二个 ListView(已经实现)中删除 ListViewItem,并在第一个 ListView 中取消选择该项目。我可以删除该项目没问题只需要在第一个 ListView 中取消选择该项目的方法
<ListView.ItemTemplate>
<DataTemplate >
<RelativePanel >
<TextBlock Text="{Binding Name}" RelativePanel.LeftOf="MoveButton"/>
<Button Name="MoveButton" Command="{Binding ElementName=NaughtySelectionList, Path=DataContext.MoveCommand}"
CommandParameter="{Binding Id}" Content="+"
Style="{StaticResource ButtonClearStyle}"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignTopWithPanel="True"/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView Name="NaughytSelectedList" ItemsSource="{Binding SelectedNaughtyChildren}">
<ListView.ItemTemplate>
<DataTemplate >
<RelativePanel >
<TextBlock Text="{Binding Name}" RelativePanel.LeftOf="MoveBackButton" />
<Button Name="MoveBackButton"
Command="{Binding ElementName=NaughytSelectedList, Path=DataContext.MoveBackCommand}"
CommandParameter="{Binding Id}" Content="✖"
Style="{StaticResource ButtonClearStyle}"
RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignTopWithPanel="True" />
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
视图模型
private Child selectedChild ;
public Child SelectedChild
{
get { return selectedChild; }
set { selectedChild = value;
base.RaisePropertyChanged();
}
}
private DelegateCommand<IList<Object>> selectionChangedCommand;
public DelegateCommand<IList<Object>> SelectionChangedCommand
=> selectionChangedCommand ?? (selectionChangedCommand = new DelegateCommand<IList<object>>(AddToNaughty, CanAddToNaughty));
private bool CanAddToNaughty(IList<object> arg)
{
return true;
}
private void AddToNaughty(IList<object> currentSelection)
{
var currentSelectionCollection= new ObservableCollection<Child>();
foreach (var child in currentSelection )
{
currentSelectionCollection.Add(child as Child);
}
foreach (var child in currentSelectionCollection)
{
if (!SelectedNaughtyChildren.Contains(child))
SelectedNaughtyChildren.Add(child);
}
//Check if an item has been deselected.
var copyOfSelectedNaughtyChildren = new ObservableCollection<Child>(SelectedNaughtyChildren);
var deselectedItems = from child in copyOfSelectedNaughtyChildren
where !currentSelectionCollection.Contains(child)
select child;
//Remove the DeselectedItem.
foreach (var child in deselectedItems)
{
SelectedNaughtyChildren.Remove(child);
}
}
private DelegateCommand<int> moveBackCommand;
public DelegateCommand<int> MoveBackCommand
=> moveBackCommand ?? (moveBackCommand = new DelegateCommand<int>(DeleteChild, CanDeleteChild));
private bool CanDeleteChild(int arg)
{
return true;
}
private void DeleteChild(int id)
{
var childMatch = from child in SelectedNaughtyChildren
where child.Id == id
select child;
var childToRemove = childMatch.First();
if (childToRemove != null)
SelectedNaughtyChildren.Remove(childToRemove);
//Hear I want to deselect the item from the first ListView
// setting selected item to null deselects all the items.
SelectedChild = null;
}
}
编辑:我添加了一些图像来显示我想要的功能。我有这个工作,但只能使用后面的代码。如果可能的话,我想在 ViewModel 中实现这一点。