title: How can I specify resources in an MVVM view model?
body: <p>Suppose I want to show list of objects where each object should have a name and a suitable image (for example MenuItems with Icons, or buttons with text and image).</p>
  
  <p>All examples and programs exposed the image in the viewmodel as a path to a PNG file and then bound the <code>Source</code> of an <code>Image</code> to that. But what if I want to use vector images (for example as a <code>DrawingImage</code> in a local <code>ResourceDictionary</code>)? Exposing the DrawingImage from the view model seems bad because I would have to store a reference to the application/window/user control/... (and it is advised to not expose such XAML objects from view models).</p>
  
  <p>So a better approach would be to use a string identifier in the view model and then somehow select the appropriate resource. If that identifier is the resource key this snippet looks tempting but does not work:</p>
  
  <pre><code>&lt;Image Source="{StaticResource {Binding Icon}}"/&gt;
  </code></pre>
  
  <p>I found two workarounds for that though they did not work for me.</p>
  
  <ol>
  <li><p>The first one was using a normal binding to the icon with a converter that looked up the resource in <code>Application.Current</code>. This does not work if the resource is stored somewhere else I think (and the situation where I initially bumped into this problem had no Application running yet since it was a Window choosing the Application to launch!).</p></li>
  <li><p><a href="http://sweux.com/blogs/psampaio/index.php/2009/06/16/using-data-binding-with-static-resources-in-wpf/" rel="noreferrer">The second workaround</a> was using a markup extension derived from <code>StaticResourceExtension</code> that fetched its <code>ResourceKey</code> from the passed binding:</p>
  
  <pre><code>&lt;Image Source="{local:BindableStaticResource {Binding Icon}"/&gt;
  </code></pre>
  
  <p>This one looks really neat because it could use local resources, also be used for other things. But when using it I always got an exception ("Resource named {FooIcon} could not be found.", showing the correct XAML file and position of the extension). Even an empty resource extension derived from <code>StaticResourceExtension</code> that just passed the resource key to the base constructor did not work and I cannot explain why. Just using <code>StaticResourceExtension</code> worked just fine.</p></li>
  </ol>
  
  <p>Any ideas how I could fix the second approach, or even better solutions?</p>
  
  <h2>Edit</h2>
  
  <p>I noticed that it <em>does</em> work when used directly like this:</p>
  
  <pre><code>&lt;Window&gt;
      &lt;Window.Resources&gt;
          &lt;DrawingImage x:Key="SomeIcon"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Image Source="{BindableStaticResource {Binding Icon}}"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>but fails for example in a <code>DataTemplate</code>. Though a normal <code>StaticResourceExtension</code> works on both occasions so I am puzzled what is going wrong.</p>