“System.Windows.Data.MultiBinding”不是属性“Text”的有效值;

发布于 2024-09-05 15:46:17 字数 542 浏览 5 评论 0原文

我正在尝试编写一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-09-12 15:46:17

不要返回 MultiBinding 本身。相反,返回 MultiBinding.ProvideValue 的结果。

顺便说一句,您在标记扩展中到底在做什么?如果您不需要覆盖 ProvideValue (已密封),也许您可​​以直接从 MultiBinding 继承。只需设置适当的 Converter 和其他属性,您几乎可以实现任何目标

Don't return the MultiBinding itself. Instead, return the result of MultiBinding.ProvideValue.

BTW, what exactly are you doing in your markup extension ? Perhaps you could just inherit from MultiBinding, if you don't need to override ProvideValue (which is sealed). You can achieve almost anything by just setting the appropriate Converter and other properties

无敌元气妹 2024-09-12 15:46:17

做了一些调查,结果发现我应该在 MarkupExtension 的 ProvideValue 方法中实际设置绑定,然后返回绑定的当前值。有点违反直觉,但到目前为止似乎有效!

我的最终结果大致如下:

public override object ProvideValue( IServiceProvider serviceProvider ) {
    IProvideValueTarget valueProvider = (IProvideValueTarget)serviceProvider.GetService( typeof( IProvideValueTarget ) );
    // only need to do this if they're needed in your logic:
    object @target = valueProvider.TargetObject;
    object @property = valueProvider.TargetProperty;

    MultiBinding result = new MultiBinding();

    // set up binding as per custom logic...

    return result.ProvideValue( serviceProvider );
}

添加一点逻辑,轻轻地进行错误处理并提供服务。

更新:事实证明,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:

public override object ProvideValue( IServiceProvider serviceProvider ) {
    IProvideValueTarget valueProvider = (IProvideValueTarget)serviceProvider.GetService( typeof( IProvideValueTarget ) );
    // only need to do this if they're needed in your logic:
    object @target = valueProvider.TargetObject;
    object @property = valueProvider.TargetProperty;

    MultiBinding result = new MultiBinding();

    // set up binding as per custom logic...

    return result.ProvideValue( serviceProvider );
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文