Declined
Last Updated: 30 Sep 2014 08:58 by ADMIN
ADMIN
Dimitar
Created on: 30 Aug 2013 07:28
Category:
Type: Bug Report
1
FIX. RadDropDownList - Setting AutoSizeItems to true causes exeption in VisualListItemFormatting
To reproduce:
- set AutoSizeItems to true.
- subscribe to VisualListItemFormatting
- cast  args.VisualItem.Data.Value to the proper object.

Workaround:
- cast only if the value is of the proper type
1 comment
ADMIN
Ivan Petrov
Posted on: 30 Sep 2014 08:06
Here is what's causing the issue:
The developer sets the AutoSizeItems property to true. Then he sets the DataSource of the RadDropDownList. Setting the data source creates the data items and starts a measure pass. When items have auto size set to true they need their visual element to determine the actual size they need. This means creating a visual item (element), adding it to the element tree and performing all operations that might affect its size like applying a theme and formatting. Triggering the formatting fires the formatting event and executes the code in its event handler. At this point the ValueMember is still not set, since the whole procedure was triggered by setting the DataSource property. When there is no ValueMember assigned to RadDropDownList the Value property of each data item returns the same as the DataBoundItem property and the cast in the formatting event handler throws an InvalidCastException.

There are several solutions to this case:
1. Always set the ValueMember, DisplayMember and other *Member properties before you set the DataSource. This is valid for all controls. Here is why. Every "member" property and the DataSource property trigger a Reset operation which recreates all data and in some cases all visual items. The member properties trigger such a reset only if there is a valid DataSource, so if you set the data source and then two member properties you get 3 Resets where if you set the DataSource last you get 1 Reset no matter how many member properties you set beforehand.

2. Always use the DataBoundItem inside formatting events. This will give you the object each item is bound to and will guarantee the same result without being affected by other properties.
MyObject obj = args.VisualItem.DataBoundItem as MyObject;

3. As my colleague has suggested in the item workaround check whether the Value property of the data item returns the correct type.