WPF ItemsControl - How to detect when an item is not visible - wpf

I'm building a WPF application to visualize a load of items (a few thousand). For this i'm using an ItemsControl of which the ItemsSource is set to a BindingList.
I've noticed that it takes some time to initialize the screen... The main bottleneck is the creation of TextBlock elements (the data template of the items contains 4 TextBlock elements). Once the screen is initialized (when all WPF elements are created), rendering is quite OK.
I've implemented zooming and panning, so i was thinking about making the appearance of the items dependent on the zoom level and the center point (a bit like Google Maps: data is only visualized for regions that are into the view).
How can this be done? Is there any way in WPF to ask the item or item container if it is currently clipped?

You can make your list virtualized. This way only the items that are visible will have UI controls created for them.
Check the ListView documentation, or this link may help:
http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx
Jogy

Related

Making a custom(/templated) ScrollViewer or ListBox that understands which item to bring to view

I am trying to make a panorama-like control in WPF c# that scrolls to/brings into view only one grid from a horizontal set of grids. I need to expose the command which allows me to scroll forward/backward from various controls (which using an InteractionTrigger would suffice) on any of the grids in the ScrollViewer or ListBox.
I want each of the internal grids to size to the actual height/width of the usercontrol they live in (like tiled pages), won't need any scrollbars, and will eventually be applying easing effects/states to each of the scroll positions.
If this is too vague/convoluted, please let me know.
Thanks in advance! :)
Easy Solution:
PathListBox. Downloaded the PathListBox toolkit (for the PathListBoxScrollBehavior), set the capacity to 1, and used the templated InteractionTriggers to bind various source objects on my grids to the InvokeCommandAction Increment/Decrement Commands. Then I added easing effects/GoToStateActions.
I realize this is sort of a cheat, but am still going to search for the "real" way to do this with custom control templates deriving from ListBox and/or ScrollViewer.

WPF Performance Degradation During UI Render

I have the following components in a WPF application:
(1) Window
(2) ContentPresenter in the Window that is bound to a property in the underlying ViewModel. This Property references another ViewModel.
(3) A DataTemplate for the ViewModel that will be bound to the ContentPresenter referenced above. This data template instantiates a third-party grid that displays some data.
Whenever the ContentPresenter renders the data from the DataTemplate, it takes approximately three to four seconds for the UI to render. This causes the UI to hang for the duration of the time that it takes to render the content. Since I have little to no control over how the third-party control renders itself - my question involves whether or not it is possible to render content in a way that the UI will not hang.
Please advise.
Thanks.
Chris
How many rows is the grid displaying? And how many of those rows are visible on screen?
I'm asking because it's possible that you've got a UI layout that defeats virtualization. Usually, controls that show a scrollable list of data will perform virtualization. (The built-in ListBox does this, and any 3rd party grid of tolerable quality should do the same.) This is critical for performance, because it means your UI only needs to instantiate those items that are actually visible, rather than everything in your list.
But it's relatively easy to defeat this virtualization by accident. One way is to wrap the list or grid control in a ScrollViewer. You need virtualizing controls to be able to manage their own scrolling for virtualization to work, so the scrolling needs to happen on the inside. Wrapping a control in a ScrollViewer prevents it from doing its own scrolling. Another way it can go wrong is if you plug in a different ItemsPanel. A third possibility is that your list/grid control actually needs to be told to use virtualization.
But if you're using a control that simply takes a long time to render just the stuff you need to show on screen, then there's not much you can do - you'd need to contact the control vendor, or consider using a different vendor...

What is the Disadvantage of VirtualizingStackPanel?

What is/are the main disadvantage of VirtualizingStackPanel? If it doesn't have any, then why it is not made as a default panel behavior/template in ItemsControl?
The MSDN page on the VirtualizingStackPanel Class has the following statements:
The word "virtualize" refers to a technique by which a subset of user interface (UI) elements are generated from a larger number of data items based on which items are visible on-screen.
and
Virtualization in a StackPanel only occurs when the items control contained in the panel creates its own item containers.
and
VirtualizingStackPanel is the default items host for the ListBox element.
From this it looks like for the "normal" use of a StackPanel as a host for buttons, text blocks etc. virtualisation wouldn't offer any advantages or might even impose a performance overhead. When used in a ListBox virtualisation does have benefits as a) item containers are created by the items control and b) there are likely to be more elements in the list than can be shown on the screen at any one time.

Animating through listbox items in Silverlight 4

I have a ListBox bound to a collection of custom objects. I have an ItemTemplate set up for displaying those items. Each item takes up a large rectangular area, so what I want to do is actually only display one item at a time. After ten seconds, I want to animate (maybe slide in/out or fade in/out) to the next item so that only that one is displayed and so on.
I can't get to grips with how to achieve this, can anyone help please?
Maybe a listbox isn't even the right approach?
If you are not dead set on using a listbox, you can get this effect using a TransitioningContentContol instead. How-to Video here (the control is in system.Windows.Controls.Layout.Toolkit). It is now part of the released toolkit (the video is old).
Part of the demo shows how to emulate exactly the behaviour you are after. You can add a slider to the side if you still want it to look a bit like a listbox (and change the selection in response to the slider value changes).

What is the difference between a stackpanel and a virtualizingstackpanel in WPF?

What is the difference between a stackpanel and a virtualizingstackpanel in WPF?
A VirtualizingStackPanel can offer performance benefits when working with very large collections. It does so by only rendering and processing a subset of the data which is visible to the user vs. processing the entire list of data. By creating only UI elements for the visible items, this can greatly reduce the amount of work it has to do.
This is really only handy though if
You are data binding non-UI elements or elements for which UI must be created in the particular panel
You are data binding a lot of data
A StackPanel on the other hand, will up front create the controls for all elements contained within the StackPanel.
The VirtualizingStackPanel MSDN page has a decent discussion: http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.aspx
This is to do with the visual tree. The virtualizingstackpanel works with things like list boxes etc to reduce the size of the visual tree by only displaying visible items - this is useful where databinding is taking place.

Resources