无法得到,如果设置的ListView ItemsContainer作为VirtualizationStackPanel正确选择项目 [英] Couldn't get selected items correctly if setting ListView ItemsContainer as VirtualizationStackPanel

查看:449
本文介绍了无法得到,如果设置的ListView ItemsContainer作为VirtualizationStackPanel正确选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把'VirtualizingStackPanel.IsVirtualizing'为true,VirtualizingStackPanel.VirtualizationMode'到'回收',因为在我的ListView的项目太多。 ListView控件的selectionMode是扩展的的ListViewItem的IsSelected属性​​绑定到我的模型的IsSelected属性​​,绑定模式是双向的。

I set 'VirtualizingStackPanel.IsVirtualizing' to true and 'VirtualizingStackPanel.VirtualizationMode' to 'Recycling', because the items in my ListView are too many. The SelectionMode of the ListView is Extended, the 'IsSelected' property of the ListViewItem is bound to 'IsSelected' property of my model, bind mode is two way.

当我想用Ctrl + A键选中所有的项目,只选择项目的一部分,所以我使用的键绑定写的选择像下面所有的方法:

When I want to use Ctrl+A to select all of the items, it only select part of the items, so I use KeyBinding to write the select all method like below:

 <KeyBinding Command="{Binding SelectAllCommand}"
                            Modifiers="Control"
                            Key="A"/>

全选方法将循环的ItemsSource集合,每个项目的IsSelected属性的设置为true。但是,这也导致了意想不到的事情。当所有的项目都被选中,我滚动滚动至底部,它会加载更多的项目到ListView,我只需一次点击一个项目,预计是其他所有项目都未被选择,只有选择此项。但是,似乎没有取消选择其它项目。

SelectAll method will loop the ItemsSource collection and set each of the item's IsSelected property to true. But it also leads to something unexpected. When all of the items are selected, I scroll the scrollbar to the bottom and it will load more items to the ListView, I single click one item and the expected is all other items are unselected, only select this item. But, it seems not unselect other items.

任何人都可以帮忙吗?

Anybody can help?

推荐答案

选择器的这种行为是可以预料的,因为它可以加载UI元素下运行。由于虚拟化技术使你只加载其中包含可视区域的元素。 So,选择不知道等。

This behaviour of the Selector is to be expected, because it can operate only with loaded UI elements. Due enabling virtualization you have loaded only those elements which contains in visible area. So, Selector does not "know" about others.

有关修复此,你必须这样做,那选择知道previously选定的项目。换句话说,你必须禁止卸载这是选择的UI元素。

For fix this you must do so, that Selector "know" about previously selected items. In other word, you must to prohibit unloading any UI element which was selected.

首先,创建自己的虚拟化面板,酒杯和妓女:

First, create own virtualizing panel with blackjack and hookers:

public class MyVirtualizingStackPanel : VirtualizingStackPanel
{
    protected override void OnCleanUpVirtualizedItem(CleanUpVirtualizedItemEventArgs e)
    {
        var item = e.UIElement as ListBoxItem;
        if (item != null && item.IsSelected)
        {
            e.Cancel = true;
            e.Handled = true;
            return;
        }

        var item2 = e.UIElement as TreeViewItem;
        if (item2 != null && item2.IsSelected)
        {
            e.Cancel = true;
            e.Handled = true;
            return;
        }

        base.OnCleanUpVirtualizedItem(e);
    }
}

接下来,在列表框,ListView控件,树视图或其他用户控制其提供替代选择默认面板。
例如,通过风格:

Next, replace default panel in the ListBox, ListView, TreeView or other user controls which provide selector. For example, via style:

<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
            <blackjackandhookers:MyVirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>

...或者,直接在您的选择:

... or, directly in your selector:

<YourSelector.ItemsPanel>
    <ItemsPanelTemplate>
        <blackjackandhookers:MyVirtualizingStackPanel/>
    </ItemsPanelTemplate>
</YourSelector.ItemsPanel>

享受!

我希望我的回答可以帮助你。

I hope my answer will help you.

这篇关于无法得到,如果设置的ListView ItemsContainer作为VirtualizationStackPanel正确选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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