1

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.

This is the XAML in the resource dictionary

<!--DatePicker Resource--> <Style x:Key="appDatePicker" TargetType="{x:Type DatePicker}"> <Setter Property="FontFamily" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontFamily}" /> <Setter Property="FontSize" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontNormalSize}" /> <Setter Property="Height" Value="Auto" /> <Setter Property="MinWidth" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlWidth}" /> <Setter Property="Background" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlBackground}" /> <Setter Property="IsEnabled" Value="True"/> </Style> <!--DatePicker Disabled Resource--> <Style x:Key="appDatePickerDisabled" TargetType="{x:Type DatePicker}" BasedOn="{DynamicResource appDatePicker}"> <Setter Property="IsEnabled" Value="False"/> </Style> 

This is the code behind to change it to the disabled style:

datepickerEDP.SetResourceReference(Control.StyleProperty, "appDatePickerDisabled"); 

and for the enabled style

datepickerEDP.SetResourceReference(StyleProperty, "appDatePicker"); 

The error I get when running this code is

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.
Source=PresentationFramework StackTrace: at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IServiceProvider serviceProvider, DependencyObject& targetDependencyObject, DependencyProperty& targetDependencyProperty) InnerException:

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 :(

2
  • make it static resource BasedOn="{StaticResource appDatePicker}". <Setter Property="IsEnabled" Value="False"/> already overrides base settings, 'DynamicResourcehas different purpose. it should fix the exception, but maybe another approach like using style triggers will be even simpler Commented Nov 19, 2016 at 14:25
  • ASh this does exactly what I need, thanks. Commented Nov 19, 2016 at 14:44

1 Answer 1

1

You don't actually want to change the resource reference. Use triggers instead:

<Style x:Key="appDatePicker" TargetType="{x:Type DatePicker}"> <Setter Property="FontFamily" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontFamily}" /> <Setter Property="FontSize" Value="{Binding Source={StaticResource userSettings}, Path=Default.userFontNormalSize}" /> <Setter Property="Height" Value="Auto" /> <Setter Property="MinWidth" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlWidth}" /> <Setter Property="Background" Value="{Binding Source={StaticResource userSettings}, Path=Default.userControlBackground}" /> <Setter Property="IsEnabled" Value="True"/> <Style.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="..."/> </Trigger> </Style.Triggers> </Style> 

If you want the value of the IsEnabled property to be changed programmatically based certain conditions, bind to a backing property that implements INotifyPropertyChanged.

Sign up to request clarification or add additional context in comments.

2 Comments

axlj, this is perfect but not for what I want to do. I will be using this though in the future. Thanks for your help.
@BU00HA it sounds like you might be working against the way WPF was designed to be used. (granted, if it works for you, then go for it). In any case, it sounds like you want to extend the datepicker control with your own 'hasBeenClicked' property. The approach of setting the resource reference will likely have unrealized side effects that you end up trying to debug for hours later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.