简体   繁体   English

C#运行时更改WPF动态资源

[英]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 这是资源字典中的XAML

<!--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'. 发生System.Windows.Markup.XamlParseException HResult = -2146233087 LineNumber = 0 LinePosition = 0消息=无法在“样式”类型的“ BasedOn”属性上设置“ DynamicResourceExtension”。 A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject. “ DynamicResourceExtension”只能在DependencyObject的DependencyProperty上设置。
Source=PresentationFramework StackTrace: at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IServiceProvider serviceProvider, DependencyObject& targetDependencyObject, DependencyProperty& targetDependencyProperty) InnerException: Source = PresentationFramework StackTrace:在MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension,IServiceProvider serviceProvider,DependencyObject&targetDependencyObject,DependencyProperty&targetDependencyProperty)内部异常:

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. 对我来说,这表明我不能使用基于样式,但是即使我更改了禁用样式以包括已启用样式中的所有内容并删除了BasedOn标签,它仍然会失败。 Does anyone have any ideas where I am going wrong? 有谁知道我要去哪里错了? This is really doing one's nut in :( 这确实是在:(

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 . 如果要基于某些条件以编程方式更改IsEnabled属性的值,请绑定到实现INotifyPropertyChanged的后备属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM