BU00HA
BU00HA

Reputation: 53

C# Runtime change WPF Dynamic Resource

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

Upvotes: 1

Views: 1885

Answers (1)

AJ X.
AJ X.

Reputation: 2770

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.

Upvotes: 1

Related Questions