如何在 WPF XAML 的多重绑定中使用 stringformat [英] How to use stringformat in multibinding in WPF XAML

查看:34
本文介绍了如何在 WPF XAML 的多重绑定中使用 stringformat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,StringFormat 对于 WPF 中的数据表示非常重要.我的问题是如何在 WPF 中进行多重绑定时使用 StringFormat?

As you know StringFormat is of great importance for data representation in WPF. My problem is how to use StringFormat when multibinding in WPF?

如果我举一个非常简单的例子:

If I give a very simple example:

我们有变量 A 和 B,其值分别为 10.255555 和 25.6999999

We have variables,which are A and B and whose values are 10.255555 and 25.6999999

我们想向他们展示10.2,25.6?

如何使用多重绑定来做到这一点?通常它是小菜一碟的 ValueConverter

How can I do this with multibinding? Normally it is piece of cake with ValueConverter

非常感谢有关此主题的任何帮助和想法

Any help and ideas on this topic will be greately appreciated

推荐答案

只需在 MultiBinding 上设置 StringFormat 属性;为多重绑定中的每个绑定使用占位符({0}、{1}...),并在必要时包含格式说明符(例如 F1 表示具有 1 个十进制数字的十进制数)

Just set the StringFormat property on the MultiBinding; use placeholders ({0}, {1}...) for each binding in the multibinding, and include format specifiers if necessary (e.g. F1 for a decimal number with 1 decimal digit)

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0:F1}{1:F1}">
            <Binding Path="A" />
            <Binding Path="B" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

开头的 {} 部分是格式字符串是一个转义序列(否则 XAML 解析器会将 { 视为标记扩展的开头)

The {} part at the beginning is the format string is an escape sequence (otherwise the XAML parser would consider { to be the beginning of a markup extension)

这篇关于如何在 WPF XAML 的多重绑定中使用 stringformat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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