Jan 3, 2012 at 3:44 AM
Edited Jan 3, 2012 at 3:46 AM
I'll bet the problem is attributable to virtualization of the cells or list items. I know you said you disabled virtualization ... but I'll bet you didn't succeed.
I've determined that Bind.Model won't work if the templated item is reused as it is in a virtuallized control such as a VirtualizingStackPanel; that's the control inside a ListBox. I suspect there is some virtualizing going on in the DataGrid
and the problem I'm about to describe is manifested upon sorting.
A virtualized items control doesn't build a visual templated item for every item in your data source. It only builds enough of them to display. When an item scrolls off the screen, the visual templated item that goes with it is re-used for displaying the
next data item, typically displayed at the bottom. SL sets the re-cycled templated item's DataContext to the new data source. But CM doesn't know that it should clear the previous bindings and establish new ones to the new data source. Therefore, the
re-cycled visual item shows the values from the original data item (whose bindings are still alive) even though the DataContext has changed to the new data item.
It isn't going to be easy to fix this, I think, because it would be tricky to distinguish between bindings that CM wrote (these should be cleared) vs. explicit bindings that you wrote (which should be preservered and would be reapplied by SL/WPF automatically).
I peeked at the source and walked quickly away. I think it could be fixed ... but it wouldn't be trivial.
Rob probably didn't encounter this problem because he followed his own advice and used explicit bindings in DataTemplates ... at least the DataTemplates in DataGrids which, I suspect, are virtualized.
In sum, it seems you will have to go back to explicit binding for templates used in a DataGrid.
I share your regret as I like to use conventions wherever they seem natural ... as they do inside a DataGrid's item template.