本文介绍了我如何从ViewModel类的ListView中获取所有选定的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Caliburn.Micro库用MVVM(C#)和XAML编写了一个程序,如何从ListView控件中获得所有选定项(不仅是一项)?
I wrote a program with MVVM (C#) and XAML using Caliburn.Micro library, how can I get all selected items from ListView control (not only one item)?
使用绑定方法SelectedItem="{Binding SelectedItem}"
刚获得第一个选定项!
With binding method SelectedItem="{Binding SelectedItem}"
just got first selected item!
推荐答案
要将选定的项放入ViewModel中,请首先在模型中创建一个bool
类型的属性,该属性将与ListViewItem
的IsSelected
属性绑定
To get selected items into the ViewModel, first create a property of bool
type in your model that will be bound with IsSelected
property of ListViewItem
.
模型类的属性:
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
RaiseChange("IsSelected");
}
}
XAML样式:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
ViewModel中的最终属性:
public List<DataGridItem> SelectedItem
{
get
{
return list.Where(item=>item.IsSelected).ToList();
}
}
这篇关于我如何从ViewModel类的ListView中获取所有选定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!