【问题标题】:How to get SelectedItems from ListView in my ViewModel [duplicate]如何在我的 ViewModel 中从 ListView 获取 SelectedItems [重复]
【发布时间】:2012-08-22 08:55:11
【问题描述】:

可能重复:
Binding SelectedItems of Listview

我有一个 ListView,用户可以选择多个项目。我需要在我的视图模型中获取从 ListView 中选择的项目列表。

请建议从 ListView 中获取 SelectedItems。

谢谢

【问题讨论】:

标签: wpf listviewitem


【解决方案1】:

我通常有两种方法

如果我只需要知道为命令的目的选择了什么,我将在ViewModel 中设置我的RelayCommandDelegateCommand 以期望IList<SomeClass> 类型的参数并传递ListView.SelectedItems作为CommandParameter

<Button Command="{Binding SomeCommand}"
        CommandParameter="{Binding ElementName=MyListView, Path=SelectedItems}" />

我经常使用的另一种方法是在ListView 中使用的任何数据项上创建一个IsSelected 属性,并将其绑定到ListViewItem.IsSelected 属性

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>

然后我的ViewModel 可以通过查看它的IsSelected 属性来确定一个项目是否被选中

foreach(var item in MyCollection)
{
    if (item.IsSelected)
        // Do work
}

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多