结合仅一个标签的一部分 [英] Binding only part of a label
本文介绍了结合仅一个标签的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个实现与在WPF绑定控件常量文本混合界值?
How would one achieve mixing bound values with constant text in a WPF bound control?
例如,说我有显示订单的形式,我想显示文本,如订单ID 1234。
For example, say I have a form displaying orders, and I want a label that displays text like "Order ID 1234".
我试着喜欢的东西:
text="Order ID {Binding ....}"
这是可以实现的,还是我不得不做一些像在流量控制有一个以上的标签?
Is this achievable, or do I have to do something like having more than one label in a flow control?
推荐答案
如果你使用3.5 SP1,您可以使用绑定的的StringFormat
属性:
If you're using 3.5 SP1, you can use the StringFormat
property on the binding:
<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>
另外,使用转换器:
Otherwise, use a converter:
<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>
使用 StringFormatConverter
作为一个的IValueConverter
:
[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : IValueConverter
{
public string StringFormat { get; set; }
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture) {
if (string.IsNullOrEmpty(StringFormat)) return "";
return string.Format(StringFormat, value);
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
这会做的伎俩。
[修改:更改文本
属性内容
]
这篇关于结合仅一个标签的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文