简体   繁体   English

当 TabControl 绑定到 ViewModels 时 WPF 很慢

[英]WPF slow when TabControl bound to ViewModels

I have TabControl bound directly to IEnumerable<ViewModelBase> (different ViewModels), rendered using DataTemplates .我将TabControl直接绑定到IEnumerable<ViewModelBase> (不同的 ViewModels),使用DataTemplates呈现。 BUT when switching Tabs, one can se that TabItems are completely redrawed and it's soooo slow.但是在切换选项卡时,可以看到TabItems完全重绘,而且速度非常慢。 Is it normal???正常吗???

Is your data context truly exposing an IEnumerable<T> as the binding source?您的数据上下文是否真正将IEnumerable<T>公开为绑定源? If so, I recommend you take a look at How Data Binding References are Resolved .如果是这样,我建议您查看如何解决数据绑定引用 This won't explicitly address the redraw issue, but if you expose your view models data source using a collection that supports the INotifyPropertyChanged interface such as ObservableCollection or a ICollectionView data source, you will get better binding and rendering performance.这不会明确解决重绘问题,但如果您使用支持INotifyPropertyChanged接口的集合(例如ObservableCollectionICollectionView数据源)公开视图模型数据源,您将获得更好的绑定和渲染性能。

On the redraw side of this issue, you should take a look at this Dr. WPF post .在这个问题的重绘方面,你应该看看这个 WPF 博士的帖子 There is a proposed solution to the performance issue you are seeing, and to go one step further you would write a TabControl subsclass, and possibly use a VirtualizingStackPanel as the items source of the custom TabControl.对于您所看到的性能问题,有一个建议的解决方案,对于 go,您将进一步编写一个 TabControl 子类,并可能使用VirtualizingStackPanel作为自定义 TabControl 的项目源。

This article goes over the UI and data virtualization options you might try.本文介绍了您可能会尝试的 UI 和数据虚拟化选项。

I would say yes, DataTemplates are blueprints of how to construct objects, so your TabControl might very well throw the old tab content and and create a new one if you switch tabs.我会说是的,DataTemplates 是如何构造对象的蓝图,因此您的 TabControl 很可能会抛出旧的选项卡内容并在切换选项卡时创建一个新内容。 Possibly this question is related.可能这个问题是相关的。

( Even though people have complained before about the behaviour that the TabControl may actually reuse objects, i suppose this could depend on the underlying type of the items ) 尽管人们之前曾抱怨 TabControl 可能实际重用对象的行为,但我想这可能取决于项目的底层类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM