简体   繁体   English

DynamicResource的后备值

[英]Fallback value for DynamicResource

My application loads all strings from resource dictionaries defined in separate XAML files. 我的应用程序从单独的XAML文件中定义的资源字典加载所有字符串。 It is possible that the DynamicResource key will not exist during run-time. 运行时可能不存在DynamicResource键。

<CheckBox Content="{DynamicResource myKeyThatIsMissing}"/>

In a normal binding there is a Fallback value that can be used like this: 在正常绑定中,有一个可以像这样使用的Fallback值:

Text="{Binding StringToShow, FallbackValue=DefaultValue}

Is it possible to have a default Fallback value for a property set with a DynamicResource that is used if the resource key does not exist? 如果资源键不存在,是否可以为使用DynamicResource的属性集设置默认的Fallback值?

The class System.Windows.DynamicResourceExtension is unsealed so perhaps you can extend it and add the logic you desire by overriding the ProvideValue method. System.Windows.DynamicResourceExtension类是未密封的,因此您可以通过重写ProvideValue方法来扩展它并添加所需的逻辑。

Unfortunately the default implementation returns an instance of the internal ResourceReferenceExpression class, so you may have to do some reflection or other magic to make this work. 不幸的是,默认实现返回内部ResourceReferenceExpression类的实例,因此您可能需要做一些反思或其他魔术才能使其工作。

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

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