Bug fix in the sample on Virtualization in WPF Control Development Unleashed

I recently received a mail from Marshall Price, one of the readers of my book WPF Control Development Unleashed, pointing at a bug in the sample from Chapter 8 (Virtualization). He had made some changes to the sample code in order to delete items from the StaggeredPanel. This caused a crash in the virtualized panel.

image

(StaggeredPanel from Chapter 8 – Virtualization)

StaggeredPanel derives from VirtualizingPanel and there is a particular virtual method, OnItemsChanged, that was not implemented. OnItemsChanged is called every time an item is added/removed/moved/replaced in the source collection. When an item is removed (the case for which the panel was crashing), the panel is supposed to delete the UI container associated with the item. This ensures that the internal data structures are in sync with the source collection.

Providing the override for OnItemsChanged avoids the crash. Notice the use of RemoveInternalChildRange to remove the associated UI container(s).

protected override voidOnItemsChanged(objectsender, ItemsChangedEventArgs args)
{
    base.OnItemsChanged(sender, args);

    switch(args.Action)
    {
        caseNotifyCollectionChangedAction.Remove:
        caseNotifyCollectionChangedAction.Move:
            RemoveInternalChildRange(args.Position.Index, args.ItemUICount);
            break;
    }
}

 

If you have encountered this crash, please add this snippet to StaggeredPanel.cs and you should be fine. Incidentally I also noticed that loading StaggeredPanelTester.xaml in Expression Blend causes an exception. To circumvent that, you need to add this line at the beginning of the method: MeasureOverride, in StaggeredPanel.cs:

if (availableSize.Width == double.PositiveInfinity || availableSize.Height == double.PositiveInfinity)
    return new Size();

This line ensures that we don’t return double.PositiveInfinity as the desired size of the panel, which is the cause of the crash in Blend.

 

If you find any other bugs in the sample code, please feel free to email me and I’ll fix it immediately.

Similar Posts:

Source code for “WPF Control Development Unleashed”

The website for my upcoming book is finally online, which also means the Source code is available for download ! Without further ado, here is the link to the website:

Untitled-2

WPF Control Development Unleashed

If you look under the “Downloads” tab, you will see the link to the zip containing the source.

To make it easier for browsing the samples, we have included a custom WPF app called “BookExplorer”. It provides a nice master-detail view of the examples, as shown in the figure below:

BookExplorer App

If you are reading the book on Safari, you will know that there are some bonus examples included. These examples have been clearly marked with “(BONUS)” in the BookExplorer app.

Hope you find the examples and the book useful for your WPF explorations! You can buy a ton of copies for your friends and family at Amazon.

Similar Posts:

Page 1 of 1412345...Last »