title: C# Runtime change WPF Dynamic Resource
body: <p>I have an issue with changing the Dynamic Resource style on a control (in the example it is a datepicker but I want to change it for a lot of different controls). I have a style for enabled and one for disabled and the disabled style is based on the enabled one. This works a treat. I want to be able to change the style when clicking a button (enabled to disabled and vice versa) but after some searching, the code I have come up with, just doesn't work.</p>
  
  <p>This is the XAML in the resource dictionary</p>
  
  <pre><code>&lt;!--DatePicker Resource--&gt;
  &lt;Style x:Key="appDatePicker" TargetType="{x:Type DatePicker}"&gt;
      &lt;Setter Property="FontFamily" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontFamily}" /&gt;
      &lt;Setter Property="FontSize" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontNormalSize}" /&gt;
      &lt;Setter Property="Height" Value="Auto" /&gt;
      &lt;Setter Property="MinWidth" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlWidth}" /&gt;
      &lt;Setter Property="Background" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlBackground}" /&gt;
      &lt;Setter Property="IsEnabled" Value="True"/&gt;
  &lt;/Style&gt;
  
  &lt;!--DatePicker Disabled Resource--&gt;
  &lt;Style x:Key="appDatePickerDisabled" TargetType="{x:Type DatePicker}" BasedOn="{DynamicResource appDatePicker}"&gt;
      &lt;Setter Property="IsEnabled" Value="False"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>This is the code behind to change it to the disabled style:</p>
  
  <pre><code>datepickerEDP.SetResourceReference(Control.StyleProperty, "appDatePickerDisabled");
  </code></pre>
  
  <p>and for the enabled style</p>
  
  <pre><code>datepickerEDP.SetResourceReference(StyleProperty, "appDatePicker");
  </code></pre>
  
  <p>The error I get when running this code is</p>
  
  <blockquote>
    <blockquote>
      <p>System.Windows.Markup.XamlParseException occurred   HResult=-2146233087   LineNumber=0   LinePosition=0   Message=A
      'DynamicResourceExtension' cannot be set on the 'BasedOn' property of
      type 'Style'. A 'DynamicResourceExtension' can only be set on a
      DependencyProperty of a DependencyObject.<br>
      Source=PresentationFramework   StackTrace:
             at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension
      markupExtension, IServiceProvider serviceProvider, DependencyObject&amp;
      targetDependencyObject, DependencyProperty&amp; targetDependencyProperty) 
      InnerException:</p>
    </blockquote>
  </blockquote>
  
  <p>This to me indicates that I can't use a based-on style but even if I changed the disabled style to include everything on the enabled style and removed the BasedOn tag it still fails. Does anyone have any ideas where I am going wrong? This is really doing one's nut in :(</p>