title: XAML image source set dynamically based on content
body: <p>Well it's not really very dynamic, at least it won't change at runtime.</p>
  
  <p>The idea is I have buttons and each one has a unique image ( icon 32x32 ). The buttons all share a style where I mess with the ControlTemplate. So each image also has 2 colors one normal and another when I mouse over.</p>
  
  <p>I noticed that when I declare the source path for the images is that they are almost all the same so I though DRY ( Don't Repeat Yourself ). What if I could use the button Name or some other property as part of the source path ( i.e. the name of the image file ). That would be good programming.</p>
  
  <p>Problem is I'm new to XAML, WPF and perhaps programming all together so I'm not sure how to do that. I guess this would need code behind or a converter of some sort ( guess I'll read about converters a bit more ). Here is a bit of code ( this doesn't work but it gives you the general idea ( hopefully )):</p>
  
  <pre><code>&lt;Style x:Key="ButtonPanelBigButton" TargetType="{x:Type Button}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="Button"&gt;
                  &lt;Border Name="ButtonBorder"
                          Height="78"
                          MaxWidth="70"
                          MinWidth="50"
                          BorderThickness="0.5"
                          BorderBrush="Transparent"
                          CornerRadius="8" &gt;
                      &lt;Grid&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="2*" /&gt;
                              &lt;RowDefinition Height="*" /&gt;
                          &lt;/Grid.RowDefinitions&gt;
  
                          &lt;!-- Here I wan't to put in the Name property of the button because there is a picture there to match --&gt;
                          &lt;Image x:Name="ButtonIcon" Source="..\Images\Icons\32x32\Blue\{Binding Name}.png" 
  
                                 Margin="4"
                                 Height="32"
                                 Width="32"
                                 HorizontalAlignment="Center" 
                                 VerticalAlignment="Center" /&gt;
  
                          &lt;TextBlock Grid.Row="1"
                                     Padding="5,2,5,2"
                                     TextWrapping="Wrap"
                                     Style="{StaticResource MenuText}"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center"&gt;
                              &lt;ContentPresenter ContentSource="Content" /&gt;                
                          &lt;/TextBlock&gt;
                      &lt;/Grid&gt;
                  &lt;/Border&gt;
  
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="True" &gt;
                          &lt;Setter TargetName="ButtonIcon" Property="Source" Value="..\Images\Icons\32x32\Green\user.png" /&gt; &lt;!-- Same Here --&gt;
                          &lt;Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{StaticResource SecondColorBrush}" /&gt;
                          &lt;Setter TargetName="ButtonBorder" Property="Background"&gt;
                              &lt;Setter.Value&gt;
                                  &lt;LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" Opacity="0.5"&gt;
                                      &lt;GradientStop Color="{StaticResource MainColor}" Offset="1" /&gt;
                                      &lt;GradientStop Color="{StaticResource SecondColor}" Offset="0.5" /&gt;
                                      &lt;GradientStop Color="{StaticResource MainColor}" Offset="0" /&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
  
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Hopefully you get where I'm going with this and someone might be able to help me so my code is nice and DRY ( now I'M repeating myself!!! ).</p>