1

Morning folks,

I've been trying to cut out some of my app's processing when I stumbled upon a suggestion on SO to load all images into a resource dictionary via BitmapImages, then referencing them rather than loading an image each time. My problem is that I need to do this all programmatically, so:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

has become:

BitmapImage bI = new BitmapImage();
Uri imgUri = new Uri(fI.FullName, UriKind.RelativeOrAbsolute);
bI.UriSource = imgUri;
DataTemplateKey dTK = new DataTemplateKey(typeof(BitmapImage));
imageDictionary.Add(dTK, bI);

Which I think should work, but as it loops due to the images loading based on database content, I immediatly get a key already added error on the second loop-through. Is there any way to create a datatemplatekey that I can name myself rather than have it based on the type?

Thanks, Becky

1 Answer 1

1

It is not possible to add a key to the datatemplate but maybe you fix your problem by creating a DataTemplateSelector. On the link below you can find very good information on how to do that:

http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.