WPF 如何在ViewModel中获取ListView 的 SelectedItems
程序员文章站
2022-07-13 23:01:50
...
貌似ListView并不支持直接对SelectedItems进行绑定,只能通过其他Command对SelectedItems进行参数传递。
方法一:通过Button的CommandParameter进行传递。
<ListBox x:Name="listbox" ItemsSource="{Binding StringList}" SelectionMode="Multiple"/>
<Button Command="{Binding GetListItemsCommand}" CommandParameter="{Binding SelectedItems, ElementName=listbox}" Content="GetSelectedListBoxItems"/>
方法二:通过System.Windows.Interactivity进行传递
xmlns:i="http://schemas.microsoft.com/expression//2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
<GridView x:Name="GridName">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Datacontext.SelectionChangedCommand, ElementName=YourUserControlName}" CommandParameter="{Binding SelectedItems, ElementName=GridName}" />
</i:EventTrigger>
</i:Interaction.Triggers>
public DelegateCommand<object> SelectionChangedCommand {get;set;}
SelectionChangedCommand = new DelegateCommand<object> (items => {
var itemList = (items as ObservableCollection<object>).Cast<YourDto>().ToList();
}
上一篇: 循环输出被选中多选框的值