1

I have a GridView that has a VirtualizingStackPanel as the ItemsPanel. The items are bound to a collection of my model objects on my viewModel. The specific properties that I am binding to in the itemtemplate are an image and three strings. When the app starts every thing works as expected. However when I scroll the items off screen and back the image disappears. The strings continue to behave as expected. If I change the ItemsPanel to a stack panel I do not have this issues so I am pretty sure that it has to do with the virtualizingstackpanel. I just have no idea where to start to debug the issue.

Any responses are much appreciated.

Thanks, James

James Pack
  • 824
  • 1
  • 10
  • 19
  • How is your item template loading the images? Through a URI reference to an embedded resource/Content image? Or, is it setting the source directly to an ImageSource property on the Item? – Nate Diamond May 12 '13 at 01:31
  • It is a StorageItemThumbnail Property on my model and I am using a converter to set the image source to a new BitmapImage; – James Pack May 12 '13 at 02:19
  • 1
    So the way the VirtualizingStackPanel works is it reuses the item container and just changes the datacontext of it. I think either your converter is not getting called each time (testable with some breakpoints and the debugger) or possibly it's reusing the same streams each time without 'rewinding' them (more likely). In your converter, try adding StorageItemThumbnailVariable.Seek(0) before getting the data out of it. If that doesn't work, see if the converter is being triggered. You may need to manually trigger it in something like OnDataContextChanged or OnLoaded. – Nate Diamond May 12 '13 at 02:32
  • I will try that. Thanks for the direction. Just a note when the app starts the converter is called everytime. I initially suspected the same as you that the converter wasn't being called and I put a breakpoint on it and it doesn't. I will look at the events you mentioned and let you know. – James Pack May 12 '13 at 03:56
  • Bingo! Thanks a lot for the help. The thumbnailStream.Seek(0) fixed it. I am a relatively new self taught programmer. Would you mind explaining to me what was going on or point me in the direction of some literature I can read up on it. Thanks again for your help! – James Pack May 12 '13 at 04:00
  • The stream reader is like a typewriter. When it gets to the end of the line, you have to push it back to the beginning, otherwise it will just always think, "Okay, I'm done." Seek(x) says "Go to stream position x", in this case, 0. And no problem! It's what this place is for ;) – Nate Diamond May 12 '13 at 05:51

1 Answers1

3

As noted in the comments by Nate Diamond, I needed to add a call to streamVariable.Seek(0) in order to make it work.

James Pack
  • 824
  • 1
  • 10
  • 19