问题描述
您知道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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!