VirtualizingStackPanel + MVVM +多重选择 [英] VirtualizingStackPanel + MVVM + multiple selection

查看:184
本文介绍了VirtualizingStackPanel + MVVM +多重选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现类似于这个帖子使用一个ViewModel存储IsSelected价值,并通过绑定 ListViewItem.IsSelected 来视图模型IsSelected:

I have implemented a selection pattern similar to the one described in this post using a ViewModel to store the IsSelected value, and by binding the ListViewItem.IsSelected to the ViewModel IsSelected:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
    </Style>
</ListView.ItemContainerStyle>

它的工作原理一般,但我遇到一个严重的问题。通过使用一个 VirtualizingStackPanel 在列表视图中的面板上,仅可见 ListViewItem的越来越创建。如果我用Ctrl + A选择所有项目,或者使用快捷键组合中的第一项,例如SHIFT + CTRL + END,所有的项目都被选择,但对于非可见项,视图模型没有得到其IsSelected设置为true。这是合乎逻辑的,因为如果 ListViewItem的不创建,绑定无法工作。

It works in general, but I encounter a severe problem. By using the a VirtualizingStackPanel as the panel in the list view, only the visible ListViewItem are getting created. If I use "Ctrl+A" to select all items, or by using shortcut combination such "Shift+Ctrl+End" on the first item, all items get selected, but for the non visible items, the ViewModel does not get its IsSelected set to true. That is logical, because if the ListViewItem are not created, the binding can't work.

任何人都经历了同样的问题,并找到了解决方法(除了不使用 VirtualizingStackPanel )?

Anybody experienced the same issue, and found a solution (apart from not using a VirtualizingStackPanel)?

推荐答案

我发现在MVVM模式,它解决了我的问题的处理选择另一种方式。相反,保持在视图模型的选择,选择从ListView控件/列表框检索,并将其作为参数的命令通过。在XAML全部完成:

I found another way of handling selection in the MVVM pattern, which solved my issue. Instead of maintaining the selection in the viewmodel, the selection is retrieved from the ListView/ListBox, and passed as a parameter to the Command. All done in XAML:

<ListView 
    x:Name="_items"
    ItemsSource="{Binding Items}" ... />

<Button 
    Content="Remove Selected" 
    Command="{Binding RemoveSelectedItemsCommand}" 
    CommandParameter="{Binding ElementName=_items, Path=SelectedItems}"/>

在我的视图模型:

private void RemoveSelection(object parameter)
{
    IList selection = (IList)parameter;
    ...
}

这篇关于VirtualizingStackPanel + MVVM +多重选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆