title: How do I use a ViewModel variable for a DynamicResource?
body: <p>This isn't MahApps.Metro-specific, but that happens to be what I'm using. I have a set of ViewModels that have a <code>string</code> property representing which icon to use from a resource XAML file.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class CommandViewModel : ViewModel
  {
      public CommandViewModel(string displayName, ICommand command, string icon)
      {
          if (command == null)
              throw new ArgumentNullException("command");
  
          DisplayName = displayName;
          Command = command;
          Icon = icon;
      }
  
      public ICommand Command { get; private set; }
      public string Icon { get; set; }
  }
  </code></pre>
  
  <p><code>Icon</code> would end up being something like "appbar_add" from MahApps.Metro.Resources. These are defined in an Icons.xaml file.</p>
  
  <p>How do I write this up in my <code>ItemTemplate</code> such that the correct resource shows. I'm either getting errors on execution (not at edit/build) or I'm getting no icons at all.</p>
  
  <p>The "no icon" XAML looks like this:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Rectangle Width="20" Height="20"&gt;
                      &lt;Rectangle.Fill&gt;
                          &lt;VisualBrush Visual="{DynamicResource {Binding Path=Icon}}" /&gt;
                      &lt;/Rectangle.Fill&gt;
                  &lt;/Rectangle&gt;
                  &lt;TextBlock Margin="15,6"&gt;                            
                      &lt;Hyperlink Command="{Binding Path=Command}"&gt;
                          &lt;TextBlock Text="{Binding Path=DisplayName}" /&gt;
                      &lt;/Hyperlink&gt;
                  &lt;/TextBlock&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>My attempts that caused errors were using <code>StaticResource</code>, which I think is fundamentally incorrect.</p>
  
  <p>How should I be referencing the <code>Icon</code> property as the name of the resource I want?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>More code was requested, so here's an example of what <em>does</em> work:</p>
  
  <pre><code>&lt;Rectangle Width="20" Height="20"&gt;
      &lt;Rectangle.Fill&gt;
          &lt;VisualBrush Visual="{StaticResource appbar_add}" /&gt;
      &lt;/Rectangle.Fill&gt;
  &lt;/Rectangle&gt;
  </code></pre>
  
  <p>What I need to do is let "appbar_add" be a value from a property on my ViewModel -- the <code>Icon</code> property above.</p>
  
  <p>The resources are in a ResourceDictionary in a separate file (Icon.xaml) and look like the following:</p>
  
  <pre><code>&lt;Canvas x:Key="appbar_add" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0"&gt;
      &lt;Path Width="38" Height="38" Canvas.Left="19" Canvas.Top="19" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z "/&gt;
  &lt;/Canvas&gt;
  </code></pre>