且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将多个值绑定到单个 WPF TextBlock?

更新时间:2021-07-06 18:13:06

您可以使用 MultiBindingStringFormat 属性.用法类似于以下内容:

You can use a MultiBinding combined with the StringFormat property. Usage would resemble the following:

<TextBlock>
    <TextBlock.Text>    
        <MultiBinding StringFormat="{}{0} + {1}">
            <Binding Path="Name" />
            <Binding Path="ID" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Name 一个 Foo 的值和 ID 一个 1 的值,你在 TextBlock 中的输出将然后是 Foo + 1.

Giving Name a value of Foo and ID a value of 1, your output in the TextBlock would then be Foo + 1.

注意:这仅在 .NET 3.5 SP1 和 3.0 SP2 或更高版本中受支持.

Note: that this is only supported in .NET 3.5 SP1 and 3.0 SP2 or later.