将 IValueConverter 与 DynamicResource 一起使用吗?
使用 DynamicResource
扩展时是否可以定义转换器? 不幸的是,其中的一些内容
<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />
给了我以下的期待:
“DynamicResourceExtension”不能 设置类型的“Source”属性 ‘绑定’。一个 “DynamicResourceExtension”只能是 设置在 a 的 DependencyProperty 上 依赖对象。
Is there a way to define a converter when using the DynamicResource
extension? Something in the lines of
<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />
which unfortunately gives me the following excpetion:
A 'DynamicResourceExtension' cannot be
set on the 'Source' property of type
'Binding'. A
'DynamicResourceExtension' can only be
set on a DependencyProperty of a
DependencyObject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道我真的很晚了,但绝对有效的是使用 BindingProxy 对于像这样的 DynamicResource
然后将转换器应用到代理
I know i am really late to this but what definitely works is using a BindingProxy for the DynamicResource like this
Then applying the converter to the proxy
尝试类似的方法:
标记扩展:
XAML:
Try something like that:
Markup extension:
XAML:
@Thomas 的帖子非常接近,但正如其他人指出的那样,它仅在执行 MarkupExtension 时执行。
这是一个执行真正绑定的解决方案,不需要“代理”对象,并且编写方式与任何其他绑定一样,只是您给它一个资源密钥而不是源和路径...
如何创建支持转换器、StringFormat 的 DynamicResourceBinding?
@Thomas's post is very close, but as others have pointed out, it only executes at the time the MarkupExtension is executed.
Here's a solution that does true binding, doesn't require 'proxy' objects, and is written just like any other binding, except instead of a source and path, you give it a resource key...
How do you create a DynamicResourceBinding that supports Converters, StringFormat?
我喜欢 mkoertgen 的回答。
下面是一个适用于我的 VB.NET 中 IValueConverter 代理的改编示例。我的资源“VisibilityConverter”现在作为 DynamicResource 包含并通过 ConverterProxy“VisibilityConverterProxy”转发。
用法:
代码:
结束命名空间
I like the answer of mkoertgen.
Here is an adapted example for a IValueConverter proxy in VB.NET that worked for me. My resource "VisibilityConverter" is now included as DynamicResource and forwarded with the ConverterProxy "VisibilityConverterProxy".
Usage:
Code:
End Namespace