【问题标题】:Bad performance ListView in TabControl WPFTabControl WPF 中性能不佳的 ListView
【发布时间】:2017-01-25 13:09:03
【问题描述】:

我有一个TabControl 和三个TabItems。每个TabItem 都有自己的ViewModel。最后一个选项卡包含带有 +1500 条记录的 ListView。所以每次我打开这个标签时,渲染都需要 +-10 秒。我想优化ListView,所以每次渲染都不需要那么长时间。

我将ObservableCollection 绑定到ListView

ListView 看起来像这样

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumnHeader>
                   <TextBox... custom templates for filtering here
                </GridViewColumnHeader>
            </GridViewColumn>
            </GridView>
    <ListView.View>
</ListView>

我已经试过了:

<VirtualizingPanel.VirtualizationMode="Recycling">

这会加快速度,但会导致滚动速度非常慢。

【问题讨论】:

    标签: wpf performance listview mvvm tabcontrol


    【解决方案1】:

    我认为您可以在几个小页面(20/50 项)上分离您的大收藏,并添加一点新项目。我可以推荐你使用 Behavior 来刷新页面。只需绑定等待命令即可将新项目添加到集合中。 向下滚动时将添加新项目。

    internal class ScrollEndToCommandBehavior : Behavior<ScrollViewer>
    {
        #region Public Static Fields and Constants
    
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof (object), typeof (ScrollEndToCommandBehavior), new PropertyMetadata(null));
    
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof (ICommand), typeof (ScrollEndToCommandBehavior), new PropertyMetadata(null));
    
        #endregion
    
        #region Public Properties
    
        public ICommand Command
        {
            get { return (ICommand) GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
    
    
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }
    
        #endregion
    
        #region Protected Methods
    
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.ViewChanged += AssociatedObjectOnViewChanged;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.ViewChanged -= AssociatedObjectOnViewChanged;
            base.OnDetaching();
        }
    
        #endregion
    
        #region Private Methods
    
        private void AssociatedObjectOnViewChanged(object sender, ScrollViewerViewChangedEventArgs eventArgs)
        {
            if (!eventArgs.IsIntermediate && Math.Abs(AssociatedObject.ScrollableHeight - AssociatedObject.VerticalOffset) < 5)
            {
                Command?.Execute(CommandParameter);
            }
        }
    
        #endregion
    }
    

    【讨论】:

    • 谢谢!!经过一些修改,这就像一个魅力。值得一提的是,ViewChanged 仅适用于 UWP。如果有人想在 WPF 中使用它,请使用 ScrollChanged。我还更改了行为,使其可以附加到ListView 本身,并在OnAttaching 中我从ListView 中提取ScrollViewer。再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2011-08-16
    • 1970-01-01
    相关资源
    最近更新 更多