Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF slow when TabControl bound to ViewModels

I have TabControl bound directly to IEnumerable<ViewModelBase> (different ViewModels), rendered using DataTemplates. BUT when switching Tabs, one can se that TabItems are completely redrawed and it's soooo slow. Is it normal???

like image 849
Cartesius00 Avatar asked Jun 05 '11 19:06

Cartesius00


2 Answers

Is your data context truly exposing an IEnumerable<T> as the binding source? 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.

On the redraw side of this issue, you should take a look at this Dr. WPF post. 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.

This article goes over the UI and data virtualization options you might try.

like image 93
Oppositional Avatar answered Nov 20 '22 13:11

Oppositional


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. 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)

like image 20
H.B. Avatar answered Nov 20 '22 13:11

H.B.