WPF 列表视图 - 选择“视野”之外的项目

发布于 2024-08-31 17:11:18 字数 505 浏览 4 评论 0原文

我正在使用 ListView 显示列表中的项目。用户可以自己选择项目,或者使用一些“预选键”来选择具有指定属性的项目。

要检查项目,我使用类似的东西:

for(int i;i<MyListView.Items.Count;++i)
{
    if( /*... Check if the items should be selected ...*/ )
        (MyListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem).IsSelected = true;
}

这对于执行时可见的项目非常有效。但对于不可见的项目,ContainerFromIndex() 返回 null。我听说这与虚拟化有关,并且列表不知道“视野”上方或下方的项目。但是,当您手动选择列表中的项目时,为什么可能会超出“视野”呢?

以及如何选择“视野”之外的项目?我认为这一定是可能的。

感谢您的帮助, 马克

I'm using a ListView to show Items in a List. The user can select the items himself, or use some 'preselect keys' to select items with specified attributes.

To check the items I use something like that:

for(int i;i<MyListView.Items.Count;++i)
{
    if( /*... Check if the items should be selected ...*/ )
        (MyListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem).IsSelected = true;
}

This works perfectly for items, that are visible at the time of excecution. But for items, that are not visible, ContainerFromIndex() returns null. I heard this has something to do with virtualization, and that the List doesn't know about items upside or downside the 'field of view'. But how comes that it is possible to have selected items in the List offside the 'field of view' when you select them manually?

And how to do a select of an item outside 'field of view'? I think that must be possible.

Thanks for any help,
Marks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-09-07 17:11:18

正如您所提到的,我的猜测是问题是 ListView 项目的虚拟化。默认情况下,ListView(和 ListBox)使用 VirtualizingStackPanel 作为其 ItemsPanel 来提高性能。有关其工作原理的简要说明,请阅读 此处

不过,您可以替换另一个面板。在这种情况下,请尝试使用普通的 StackPanel。如果 ListView 中有大量项目,特别是复杂的项目,性能可能会受到一些影响。

<ListView>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

编辑

您还可以尝试此类似问题,取决于您的型号。但是,这可能对您不起作用。

As you mentioned, my guess is that the problem is virtualization of the ListView items. By default, ListView (and ListBox) use VirtualizingStackPanel as their ItemsPanel to improve performance. A brief explanation for how it works can be read here.

You can substitute another Panel, however. In this case, try using a normal StackPanel. If you have a ton of items in the ListView, especially if they are complex items, performance might suffer a little.

<ListView>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

Edit

You can also try to solution described at this similar question, depending on your model. However, this probably won't work for you.

飞烟轻若梦 2024-09-07 17:11:18

在处理虚拟化项目控件时,禁用虚拟化的替代方法(实际上,有时这是一个有用的功能,即使它会干扰 API 其他部分的正确操作),就是找到 VirtualizingPanel并明确告诉它滚动。

例如:

void ScrollToIndex(ListBox listBox, int index)
{
    VirtualizingPanel panel = FindVisualChild<VirtualizingPanel>(listBox);

    panel.BringIndexIntoViewPublic(index);
}

static T FindVisualChild<T>(DependencyObject o) where T : class
{
    T result = o as T;

    if (result != null)
    {
        return result;
    }

    int childCount = VisualTreeHelper.GetChildrenCount(o);

    for (int i = 0; i < childCount; i++)
    {
        result = FindVisualChild<T>(VisualTreeHelper.GetChild(o, i));

        if (result != null)
        {
            return result;
        }
    }

    return null;
}

我不太满意需要通过视觉树搜索来查找面板,但我不知道有任何其他方式来获取它,也不知道在处理虚拟化面板时滚动到特定索引。

When dealing with virtualizing items controls, an alternative to disabling virtualization (which is in fact a useful feature at times, even if it interferes with correct operation of other parts of the API), is to find the VirtualizingPanel and tell it explicitly to scroll.

For example:

void ScrollToIndex(ListBox listBox, int index)
{
    VirtualizingPanel panel = FindVisualChild<VirtualizingPanel>(listBox);

    panel.BringIndexIntoViewPublic(index);
}

static T FindVisualChild<T>(DependencyObject o) where T : class
{
    T result = o as T;

    if (result != null)
    {
        return result;
    }

    int childCount = VisualTreeHelper.GetChildrenCount(o);

    for (int i = 0; i < childCount; i++)
    {
        result = FindVisualChild<T>(VisualTreeHelper.GetChild(o, i));

        if (result != null)
        {
            return result;
        }
    }

    return null;
}

I'm not very happy with the need to search through the visual tree to find the panel, but I'm not aware of any other way to get it, nor to scroll to a specific index when dealing with a virtualizing panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文