Render UI Using BackgroundWorker
I am binding my ContentPresenter to a ViewModel that has a type-referential DataTemplate which contains an instance of a third-party control (DevExpress' GridControl). When this control is bound to a modestly sized collection (i.e. 1000 items), the control takes a noticable four or five seconds to load. So, to my question - for controls that take a while to render, can this somehow be done using a BackgroundWorker such that the UI doesn't hang? Keep in mind that I my controls reside in a DataTemplate, so any code-behind is not a desirab开发者_开发百科le option.
Thanks!
Unfortunately creating the actual UI (in your case, creating, positioning, and rendering controls) must be done on the UI thread due to compatibility constraints - all UI components must be created by the UI thread, and they can only be altered by the UI thread as well.
One thing I would recommend looking at is virtualization - if you're not displaying the datatemplate of 1000 items, why create all of the controls? You can find examples around like Virtualized WPF Canvas, or using the built-in VirtualizingStackPanel
. Using these techniques will be more work than simply binding a viewmodel to an item with a datatemplate, but will give much, much better performance.
If the actual issue is just that the DevExpress GridControl is super slow with 1000 items, then you'll want to see if you can set it to a virtual mode, or switch to a different 3rd party control.
Your ItemsControl
should contain a VirtualizingStackPanel
which ListView and ListBox do, but make sure that virtualisation is switched on and you use container recylcling
<ItemsControl
VirtualizingStackPanel.IsVirtualizing="true"
VirtualizingStackPanel.VirtualizationMode="Recycling">
精彩评论