是否可以为静态资源动态创建 ResourceKey?(例如使用绑定) [英] Is it possible to dynamically create a ResourceKey for a StaticResource? (eg using a Binding)
问题描述
假设我要绑定的对象有一个属性,该属性带有一个表示 ResourceKey 的字符串 - 我如何根据与底层对象的绑定让 StaticResource 动态获取它的 ResourceKey?
Suppose the object I'm binding to has a property with a string representing the ResourceKey - how do I get a StaticResource to dynamically acquire it's ResourceKey based on a binding to the underlying object?
我想要与此相同的东西
MyProperty="{StaticResource ResourceKey={Binding Path=MyProperty}}"
编译时,它会失败,抱怨找不到 System.Windows.Data.Binding 类型的键
While this compiles, it will fail complaining it can't find a key of type System.Windows.Data.Binding
如果基础值发生变化(例如 DynamicResource),我不需要动态重新评估
I don't need dynamic re-evaluation if the underlying value changes (eg DynamicResource)
推荐答案
我在这里找到了我想要的东西:
I found exactly what I was looking for here:
http://blog.functionalfun.net/2009/12/specifying-resource-keys-using-data_31.html
这是一个自定义标记扩展,其行为类似于常规绑定,但 Path 将指向将目标转换为 ResourceKey 的属性,然后绑定将返回资源!
It's a custom markup extension that behaves like a regular binding but the Path will point to a property that transforms the target into a ResourceKey and the binding will then return the resource!
我发现这非常有用!
这篇关于是否可以为静态资源动态创建 ResourceKey?(例如使用绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!