问题描述
或者,更清楚地说,我如何格式化文本块(在我的情况下,包含在工具提示中),以便文本的某些部分来自绑定值.
Or, to be more clear, how can I format a block of text (in my case, to be included within a tooltip) such that some portions of the text come from bound values.
在普通的 C# 中,我会使用:
In plain C# I would use:
_toolTip.Text = string.Format("{1:#0}% up, {2:#0}% down",
Environment.NewLine, percentageOne, percentage2);
但是,Text 属性的 WPF XAML 标记似乎只能包含一个绑定.花括号给了我很高的期望,但这是不可能的:
However the WPF XAML markup for a Text property seems able to contain only a single binding. The curly braces gave me high hopes, but this isn't possible:
<Element>
<Element.Tooltip>
<!-- This won't compile -->
<TextBlock Text="{Binding Path=PercentageOne}% up, {Binding Path=PercentageTwo}% down"/>
</Element.Tooltip>
</Element>
我读到 Run.Text
属性不是依赖属性,因此无法绑定.
I read that the Run.Text
property is not a dependency property and can therefore not be bound.
有没有办法在 XAML 中执行这种格式设置?
Is there a way I can perform this formatting in XAML?
推荐答案
可以使用 MultiBinding + StringFormat(需要 WPF 3.5 SP1):
You can use MultiBinding + StringFormat (requires WPF 3.5 SP1):
<TextBox.Text>
<MultiBinding StringFormat="{}{1:#0}% up, {2:#0}% down">
<Binding Path="PercentageOne" />
<Binding Path="PercentageTwo"/>
</MultiBinding>
</TextBox.Text>
关于 Run.Text - 您无法绑定到它,但有一些解决方法:
Regarding Run.Text - you can't bind to it but there are some workarounds:
- http://fortes.com/2007/03/20/bindablerun/
- http://paulstovell.net/blog/index.php/attached-bindablerun/
这篇关于什么是 String.Format 的 WPF XAML 数据绑定等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!