1

I have a few images in a folder 'Images' in my solution. From within a class in my library I would like to decide which image I should use at runtime. The old way i would do this from within a user control's code behind is by loading all of the images in the class's constructor like so:

myImage = FindResource("MyImageResource") as BitmapImage;

Then determining at runtime which image to use.

But this doesn't seem to work from within any class in my class library not to mention it seems to go against all that I've read about MVVM.

A bit more information on what i am trying to do Essentially i have a model class which stores all of the information on a drive which has been inserted such as drive type and its name.

A view model class which monitors when any drive is inserted or removed.

And the view class which is a panel that holds a collection of buttons resembling the drives of the system.

To Clarify my question:

How should i store and load images in a WPF class library?

In which class should i determine (at runtime) which image to use (in the MVVM pattern)?

Timmoth
  • 480
  • 2
  • 18
  • 1
    The question is very broad. In general, you would load an image resource from any loaded assembly by a [Resource File Pack URI](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.100).aspx#Resource_File_Pack_URIs___Local_Assembly). – Clemens Mar 30 '16 at 10:34
  • If this is a custom user control you've written than you shouldn't be using MVVM pattern - just keep all your logic in your code behind – auburg Mar 30 '16 at 10:49
  • Can you explain why that is the case please @auburg ? – Timmoth Mar 30 '16 at 10:52
  • see this discussion http://stackoverflow.com/questions/2637662/wpf-user-control-hell-with-mvvm-and-dependency-properties – auburg Mar 30 '16 at 10:58

1 Answers1

0

What i have decided to do thus far is to give the model class a BitmapImage property which i set in it's constructor to the Image that represents its drive type using this line:

DiskImage  = new BitmapImage(new Uri("/MyApp;component/Images/DiskDrive.png", UriKind.Relative));
Timmoth
  • 480
  • 2
  • 18