问题描述
或者,更清楚,我怎么可以格式化文本块(在我的情况,被列入工具提示中),使得文本的某些部分来自绑定的值。
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#我会用:
_toolTip.Text = string.Format("{1:#0}% up, {2:#0}% down",
Environment.NewLine, percentageOne, percentage2);
然而,WPF XAML标记的Text属性似乎只能够包含一个单一的结合。大括号给了我很高的期望,但这是不可能的:
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/
- http://fortes.com/2007/03/20/bindablerun/
- http://paulstovell.net/blog/index.php/attached-bindablerun/
这篇关于什么是的String.Format的WPF XAML数据绑定相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!