我正在开发一个 UWP 应用。 我有一个 ListView ,其中 ListView 项有一个复选框和内容。我需要实现的是,当我选中复选框时,会选择相关的 ListView 项;当我取消选中它时,相关的 ListView 项将被取消选择。我的 ListView 需要支持多选。 这是我的 xmal 代码:
<ListView Grid.Row="1" x:Name="SuggestListView" ItemsSource="{Binding SuggestList}" IsMultiSelectCheckBoxEnabled="True" IsItemClickEnabled="True" SelectionChanged="SuggestListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ctl:PersonUserControl HorizontalAlignment="Left"/>
<CheckBox Name="CheckBoxhhh" HorizontalAlignment="Right" IsChecked="{Binding IsSelected, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="SelectionChanged">
<Core:InvokeCommandAction Command="{Binding SelectSuggestPersonCommand}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ListView>
有人能给我一些灯吗?
最佳答案
您的思路是正确的,但如果您想在 IsMultiSelectCheckBoxEnabled 设置为 true 的情况下使用此功能,则无需在 ItemTemplate 中实现您自己的复选框。
根据我从您对该问题的评论中收集到的信息,您正在寻找一种在从另一个集合中进行选择时将项目从一个集合转移到另一个集合的方法。
因此,删除该复选框并将 SelectionMode="Multiple"添加到您的 ListView 中。
在 ListView 的行为中,您正在监听 SelectionChanged,因此将其从 ListView 中删除,它应该如下所示:
<ListView Grid.Row="1" x:Name="SuggestListView" ItemsSource="{Binding SuggestList}" IsMultiSelectCheckBoxEnabled="True" SelectionMode="Multiple">
<ListView.ItemTemplate>
<DataTemplate>
<ctl:PersonUserControl HorizontalAlignment="Left"/>
</DataTemplate>
</ListView.ItemTemplate>
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="SelectionChanged">
<Core:InvokeCommandAction Command="{Binding SelectSuggestPersonCommand}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ListView>
然后,在您在行为中使用的 SelectionChanged 事件的绑定(bind)命令中,您需要将添加和删除的项目添加到 ObservableCollection 中,您可以从其他 ListView 绑定(bind)到该项目以显示选定的项目。
该方法看起来像这样:
public ObservableCollection<ItemType> SelectedItems { get; private set; }
private void SelectedItemsChanged(SelectionChangedEventArgs args)
{
foreach (var item in args.AddedItems)
{
var vm = item as ItemType;
if (vm == null)
{
continue;
}
this.SelectedItems.Add(vm);
}
foreach (var item in args.RemovedItems)
{
var vm = item as ItemType;
if (vm == null)
{
continue;
}
this.SelectedItems.Remove(vm);
}
}
关于xaml - 检查复选框 = listviewitem 行被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261247/