TabControl WPF 中的 ListView 性能不佳
Bad performance ListView in TabControl WPF
我有一个 TabControl
和三个 TabItems
。每个 TabItem
都有自己的 ViewModel。最后一个选项卡包含一个 ListView
和 +1500 条记录。所以每次我打开这个选项卡,渲染都需要 +-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">
这会加快速度,但会使滚动变得非常慢。
我认为您可以将大 collection 放在几个小页面(20/50 项)上,然后一点点添加新项。我可以建议您使用 Behavior 来刷新页面。只需绑定等待命令即可将新项目添加到 collection。
向下滚动时将添加新项目。
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
}
我有一个 TabControl
和三个 TabItems
。每个 TabItem
都有自己的 ViewModel。最后一个选项卡包含一个 ListView
和 +1500 条记录。所以每次我打开这个选项卡,渲染都需要 +-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">
这会加快速度,但会使滚动变得非常慢。
我认为您可以将大 collection 放在几个小页面(20/50 项)上,然后一点点添加新项。我可以建议您使用 Behavior 来刷新页面。只需绑定等待命令即可将新项目添加到 collection。 向下滚动时将添加新项目。
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
}