如何控制一个ListBox中的MVVM WPF应用程序的滚动位置 [英] How to control the scroll position of a ListBox in a MVVM WPF app

查看:2113
本文介绍了如何控制一个ListBox中的MVVM WPF应用程序的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的列表框与垂直滚动启用,我的MVVM有新的和编辑个ICommand。
我加入新项目到集合的结束,但我想也滚动条自动位置到结束的时候我打电话给我的MVVM-AddCommand。
我也正在编辑一个项目(通过调用EditCommand与特定行项目)的应用程序,这样我ListBoxItem的获得在编辑使用DataTrigger模式,但我怎么会带来一些其他部分特定行(ListBoxItem的)到视图通过调整滚动位置。

I have got a big ListBox with vertical scrolling enabled, my MVVM has New and Edit ICommands. I am adding new item to the end of the collection but I want the scrollbar also to auto position to the End when I call my MVVM-AddCommand. I am also making an item editable(By calling EditCommand with a particular row item) from some other part of the application so that my ListBoxItem getting in to edit mode using DataTrigger, but how will I bring that particular row(ListBoxItem) to the view by adjusting the scroll position.

如果我在观察侧做,我可以调用listBox.ScrollInToView(lstBoxItem)。
但是,什么是从一个角度MVVM解决这个共同的滚动问题的最佳途径。

If I am doing it in the View side I can call listBox.ScrollInToView(lstBoxItem). But what is the best way to solve this common Scroll issue from an MVVM perspective.

推荐答案

我通常设置 IsSynchronizedWithCurrentItem =真的ListBox 。然后,我添加的SelectionChanged 处理程序,并始终把所选项目到视图,code是这样的:

I typically set IsSynchronizedWithCurrentItem="True" on the ListBox. Then I add a SelectionChanged handler and always bring the selected item into view, with code like this:

    private void BringSelectionIntoView(object sender, SelectionChangedEventArgs e)
    {
        Selector selector = sender as Selector;
        if (selector is ListBox)
        {
            (selector as ListBox).ScrollIntoView(selector.SelectedItem);
        }
    }

这是我的虚拟机,我可以得到默认集合查看和使用的 MoveCurrent *()方法之一,以确保该项目正在编辑的当前项目。

From my VM I can get the default collection view and use one of the MoveCurrent*() methods to ensure that the item being edited is the current item.

CollectionViewSource.GetDefaultView(_myCollection).MoveCurrentTo(thisItem);

请注意:编辑使用 ListBox.ScrollIntoView()来适应虚拟化

NOTE: Edited to use ListBox.ScrollIntoView() to accomodate virtualization

这篇关于如何控制一个ListBox中的MVVM WPF应用程序的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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