“System.Windows.Data.MultiBinding”不是属性“Text”的有效值;
我正在尝试编写一个自定义 MarkupExtension
,它允许我使用自己的机制来定义绑定,但是当我尝试从我的 MarkupExtension 返回
我得到了上面的异常。MultiBinding
时
我有:
<TextBlock Text="{my:CustomMarkup ...}" />
CustomMarkup
返回一个MultiBinding
,但显然Text
不喜欢被设置为MultiBinding
。 时,它为什么行得通?
<TextBlock>
<TextBlock.Text>
<MultiBinding ... />
</TextBlock.Text>
</TextBlock>
当我说:但它按照我的方式行不通
I'm trying to write a custom MarkupExtension
that allows me to use my own mechanisms for defining a binding, however when I attempt to return a MultiBinding
from my MarkupExtension
I get the above exception.
I have:
<TextBlock Text="{my:CustomMarkup ...}" />
CustomMarkup
returns a MultiBinding
, but apparently Text
doesn't like being set to a MultiBinding
. How come it works when I say:
<TextBlock>
<TextBlock.Text>
<MultiBinding ... />
</TextBlock.Text>
</TextBlock>
But it doesn't work the way I'm doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要返回
MultiBinding
本身。相反,返回MultiBinding.ProvideValue
的结果。顺便说一句,您在标记扩展中到底在做什么?如果您不需要覆盖
ProvideValue
(已密封),也许您可以直接从MultiBinding
继承。只需设置适当的Converter
和其他属性,您几乎可以实现任何目标Don't return the
MultiBinding
itself. Instead, return the result ofMultiBinding.ProvideValue
.BTW, what exactly are you doing in your markup extension ? Perhaps you could just inherit from
MultiBinding
, if you don't need to overrideProvideValue
(which is sealed). You can achieve almost anything by just setting the appropriateConverter
and other properties做了一些调查,结果发现我应该在 MarkupExtension 的 ProvideValue 方法中实际设置绑定,然后返回绑定的当前值。有点违反直觉,但到目前为止似乎有效!
我的最终结果大致如下:
添加一点逻辑,轻轻地进行错误处理并提供服务。
更新:事实证明,MultiBinding.ProvideValue() 根据
serviceProvider
中的目标和属性信息挂钩绑定本身。这样就干净多了。Did some investigating, and it turns out I'm supposed to actually set the binding in the MarkupExtension's ProvideValue method and then return the current value of the binding. A little counter-intuitive but so far seems to be working!
Here's roughly what I ended up with:
Add a little logic, dust lightly with error handling and serve.
Update: Turns out MultiBinding.ProvideValue() hooks up the binding itself, based on the target and property information in
serviceProvider
. That's much cleaner.