C# WPF ListView 选择多个列表视图项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ListView Selecting Multiple List View Items
提问by Kevin
I am figuring out a way to Select Multiple items in list view and deleting them on a certain action. What I can't figure out is, how should I have these multiple items selected? I would think there is a list that I would need to add them all into, but what's the best way to approach this situation, do you have any ideas? Thanks! -Kevin
我正在想办法在列表视图中选择多个项目并在某个操作中删除它们。我想不通的是,我应该如何选择这些多个项目?我认为有一个列表需要将它们全部添加到其中,但是处理这种情况的最佳方法是什么,您有什么想法吗?谢谢!-凯文
回答by Arcturus
Set SelectionModeto Multiple
or Extended
and iterate through theSelectedItems
in your ListView
.
将SelectionMode设置为Multiple
orExtended
并SelectedItems
在您的ListView
.
回答by Amsakanna
You can do one of the following...
您可以执行以下操作之一...
ListView.SelectionMode = SelectionMode.Extended
in code-behind or
ListView.SelectionMode = SelectionMode.Extended
在代码隐藏或
<ListView SelectionMode="Extended"></ListView>
in XAML
<ListView SelectionMode="Extended"></ListView>
在 XAML 中
you also have 'multiple' selectionMode yet you could rather go for 'extended' which allows the user to select multiple items only using shift modifier.
您也有 'multiple' selectionMode 但您宁愿选择 'extended',它允许用户仅使用 shift 修饰符来选择多个项目。
For deleting the items selected you could use the ListView.SelectedItems Propery as follows
要删除选定的项目,您可以使用 ListView.SelectedItems 属性,如下所示
while( myListView.SelectedItems.Count > 0 )
{
myListView.Items.Remove(list.SelectedItems[0]);
}
[or you could use the SelectedIndices property]
[或者您可以使用 SelectedIndices 属性]
Hope this will avoid the issue you encountered :)
希望这将避免您遇到的问题:)
Cheers!
干杯!
回答by Tigran
I would suggest do not use the SelectedItems
property of ListView
, instead bind the Selected
property of the single ListViewItem
, to a corresponding ViewModel
class. After this, the only thing you need to do is to find all ViewModel
object that have bound the Selected
property TRUE, remove them from model collection (if you do remove) and refresh UI. If the collection is ObservableCollection
, the UI will be refreshed automatically.
Good Luck.
我建议不要使用 的SelectedItems
属性ListView
,而是将Selected
single的属性绑定ListViewItem
到相应的ViewModel
类。在此之后,您唯一需要做的就是找到所有ViewModel
绑定Selected
属性为 TRUE 的对象,将它们从模型集合中删除(如果您确实删除了)并刷新 UI。如果集合是ObservableCollection
,UI 将自动刷新。祝你好运。
回答by Refat Sardar
Get success also WPF listview by writing
通过写作也获得成功 WPF listview
while (lvJournalDetails.SelectedItems.Count > 0)
{
lvJournalDetails.Items.Remove(lvJournalDetails.SelectedItem);
}
回答by David Refaeli
Arcturus answer is good if you're not using MVVM. But if you do and your ItemsSource is binded to some ObservableCollection of objects in your ViewModel, I would recommend Tigran answer, with Noman Khan clarification.
如果您不使用 MVVM,Arcturus 的回答很好。但是如果你这样做并且你的 ItemsSource 被绑定到你的 ViewModel 中的一些 ObservableCollection 对象,我会推荐 Tigran 回答,Noman Khan 澄清。
This is how it would look like:
这是它的样子:
<ListView ItemsSource="{Binding SomeListViewList}">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding SomeItemSelected, Mode=TwoWay}" />
</Style>
</ListView.Resources>
...
</ListView>
In View Model you would have object: public ObservableCollection<SomeItem> SomeListViewList{ get; set; }
在视图模型中,您将拥有对象: public ObservableCollection<SomeItem> SomeListViewList{ get; set; }
SomeItem Class would include a Selected property:
SomeItem 类将包含 Selected 属性:
public class SomeItem
{
public string SomeItemName { get; set; }
public string SomeItemNum { get; set; }
public bool SomeItemSelected { get; set; }
}
Then you could iterate/run over the list and get the ones that are selected:
然后你可以迭代/运行列表并获取选择的列表:
foreach (var item in SomeListViewList)
if (item.SomeItemSelected)
// do something