title: Dynamic Style Binding with IValueConverter
body: <p>I am trying to set a style, which is defined in the <code>App.xaml</code>, dynamically when loading a user control and it's just not applying the style for some reason (i.e. there is no error occurring, it's just not applying the style).</p>
  
  <p>I'm sure it's because I've defined the binding wrong, but I'm unable to figure out what I need to do differently to get it to work.</p>
  
  <h1>App.xaml Style</h1>
  
  <p>The style I'm after is the <code>RunningTitleBlock</code> and it's comprised of a couple other styles that I've included in the below code sample.</p>
  
  <pre><code>&lt;Style TargetType="Label"&gt;
      &lt;Setter Property="Margin" Value="4"/&gt;
  &lt;/Style&gt;
  
  &lt;Style TargetType="Label"
         BasedOn="{StaticResource {x:Type Label}}"
         x:Key="HeaderBlock"&gt;
      &lt;Setter Property="FontSize" Value="16"/&gt;
      &lt;Setter Property="FontWeight" Value="Bold"/&gt;
      &lt;Setter Property="Foreground" Value="White"/&gt;
  &lt;/Style&gt;
  
  &lt;Style TargetType="Label"
         BasedOn="{StaticResource ResourceKey=HeaderBlock}"
         x:Key="TitleBlock"&gt;
      &lt;Setter Property="Foreground" Value="Black"/&gt;
  &lt;/Style&gt;
  
  &lt;Style TargetType="Label"
         BasedOn="{StaticResource ResourceKey=TitleBlock}"
         x:Key="RunningTitleBlock"&gt;
      &lt;Setter Property="Background"&gt;
          &lt;Setter.Value&gt;
              &lt;LinearGradientBrush StartPoint="0.0, 0.5"
                                   EndPoint="1.0, 0.5"&gt;
                  &lt;GradientStop Color="White" Offset="0.0"/&gt;
                  &lt;GradientStop Color="Green" Offset="1.0"/&gt;
              &lt;/LinearGradientBrush&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <h1>Binding on the user control</h1>
  
  <p>I'm trying to get the <code>Binding</code> to bind to a value returned from a value converter.</p>
  
  <pre><code>Style="{DynamicResource ResourceKey={Binding Path=MonitoringType, Converter={StaticResource TSConverter}}}"
  </code></pre>
  
  <h1>Code</h1>
  
  <h2>MonitoringTypes Enum</h2>
  
  <pre><code>public enum MonitoringTypes
  {
      Running,
      Failed,
      Paused,
      Favorites,
  }
  </code></pre>
  
  <h2>User Control</h2>
  
  <p>Here what I'm trying to do is concatenate the string value of the <code>MonitoringTypes</code> enum value that's passed in with some well known text to build a style name that exists in the <code>App.xaml</code>. The value converter is being called <strong><em>and</em></strong> returning the correct value, but for some reason the style isn't applying.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interaction logic for MonitorWorkflow.xaml
  /// &lt;/summary&gt;
  public partial class MonitorWorkflow : UserControl
  {
      public MonitorWorkflow(MonitoringTypes monitoringType)
      {
          InitializeComponent();
  
          this.DataContext = new MonitorWorkflowViewModel { MonitoringType = monitoringType };
      }
  }
  
  public class MonitorWorkflowViewModel
  {
      public MonitoringTypes MonitoringType { get; set; }
  }
  
  public class TitleStyleValueConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          var type = (MonitoringTypes)value;
          return string.Format("{0}TitleBlock", Enum.GetName(typeof(MonitoringTypes), type));
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return Enum.Parse(typeof(MonitoringTypes), value.ToString().Substring(0, value.ToString().IndexOf("TitleBlock")));
      }
  }
  </code></pre>