How to get SelectedItems from ListView in my ViewModel [duplicate]

℡╲_俬逩灬. 提交于 2019-12-08 07:03:09

问题


Possible Duplicate:
Binding SelectedItems of Listview

I have a ListView and User can select multiple items. I need to get list of items selected from ListView in my View Model.

please suggest to get SelectedItems from ListView.

Thank you


回答1:


There are two ways I usually do this

If I only need to know what is selected for the purpose of a command, I will setup my RelayCommand or DelegateCommand in the ViewModel to expect a parameter of type IList<SomeClass> and pass the ListView.SelectedItems in as the CommandParameter

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

The other method I often use is to create an IsSelected property on whatever data item is being used in the ListView, and bind it to the ListViewItem.IsSelected property

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

Then my ViewModel can find out if an item is selected or not by looking at it's IsSelected property

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


来源:https://stackoverflow.com/questions/12069521/how-to-get-selecteditems-from-listview-in-my-viewmodel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!