如何在多绑定中为 1 绑定传递常量值? [英] How can I pass a constant value for 1 binding in multi-binding?

查看:13
本文介绍了如何在多绑定中为 1 绑定传递常量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像多绑定

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myConverter}">
            <Binding Path="myFirst.Value" />
            <Binding Path="mySecond.Value" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

我想传递一个固定值,例如123"到上述两个绑定之一.我如何使用 XAML 做到这一点?

And I want to pass a fixed value e.g. "123" to one of the two bindings above. How can I do that using XAML?

推荐答案

如果您的值只是一个 string,您可以在 Source 属性中将其指定为常量的绑定.如果是其他任何原始数据类型,则需要定义一个静态资源并引用它.

If your value is simply a string, you can specify it as a constant in the Source property of a binding. If it is any other primitive data type, you need to define a static resource and reference this.

在 XAML 的根目录中定义 sys 命名空间以指向 mscorlib 中的 System,以下应该可以工作:

Define the sys namespace in the root of the XAML to point to System in mscorlib, and the following should work:

<TextBlock>
  <TextBlock.Resources>
    <sys:Int32 x:Key="fixedValue">123</sys:Int32>
  </TextBlock.Resources>
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myConverter}">
      <Binding Path="myFirst.Value" />
      <Binding Source="{StaticResource fixedValue}" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

这篇关于如何在多绑定中为 1 绑定传递常量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆