[英]WPF StringFormat on Label Content
I want to format my string binding as Amount is X
where X
is a property bound to a label.我想将我的字符串绑定格式化为Amount is X
,其中X
是绑定到标签的属性。
I've seen many examples but the following doesn't work:我看过很多例子,但以下不起作用:
<Label Content="{Binding Path=MaxLevelofInvestment,
StringFormat='Amount is {0}'}" />
I've also tried these combinations:我也试过这些组合:
StringFormat=Amount is {0}
StringFormat='Amount is {}{0}'
StringFormat='Amount is \{0\}'
I even tried changing the binding property's datatype to int
, string
and double
.我什至尝试将绑定属性的数据类型更改为int
、 string
和double
。 Nothing seems to work.似乎没有任何效果。 This is a very common use case but doesn't seem to be supported.这是一个非常常见的用例,但似乎不受支持。
The reason this doesn't work is that the Label.Content
property is of type Object
, and Binding.StringFormat
is only used when binding to a property of type String
.这不起作用的原因是Label.Content
属性是Object
类型,而Binding.StringFormat
仅在绑定到String
类型的属性时使用。
What is happening is:正在发生的事情是:
Binding
is boxing your MaxLevelOfInvestment
value and storing it the Label.Content
property as a boxed decimal value.该Binding
是拳击您MaxLevelOfInvestment
值并将其存储在Label.Content
财产作为套装十进制值。ContentPresenter
. Label 控件有一个包含ContentPresenter
的模板。ContentTemplate
is not set, ContentPresenter
looks for a DataTemplate
defined for the Decimal
type.由于未设置ContentTemplate
, ContentPresenter
查找为Decimal
类型定义的DataTemplate
。 When it finds none, it uses a default template.当它没有找到时,它使用默认模板。ContentPresenter
presents strings by using the label's ContentStringFormat
property. ContentPresenter
使用的默认模板通过使用标签的ContentStringFormat
属性来呈现字符串。Two solutions are possible:有两种解决方案:
Here is how to use Label.ContentStringFormat:下面是如何使用 Label.ContentStringFormat:
<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" />
Here is how to use a TextBlock:下面是如何使用 TextBlock:
<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" />
Note: For simplicity I omitted one detail in the above explanation: The ContentPresenter
actually uses its own Template
and StringFormat
properties, but during loading these are automatically template-bound to the ContentTemplate
and ContentStringFormat
properties of the Label
, so it seems as if the ContentPresenter
is actually using the Label
's properties.注意:为了简单起见,我在上面的解释中省略了一个细节: ContentPresenter
实际上使用了自己的Template
和StringFormat
属性,但是在加载过程中,这些属性会自动绑定到Label
的ContentTemplate
和ContentStringFormat
属性,因此看起来好像ContentPresenter
实际上是在使用Label
的属性。
Make a universal StringFormatConverter : IValueConverter
.制作一个通用的StringFormatConverter : IValueConverter
。 Pass your format string as ConverterParameter
.将您的格式字符串作为ConverterParameter
传递。
Label Content="{Binding Amount, Converter={...myConverter}, ConverterParameter='Amount is {0}'"
Also, make StringFormatMultiConverter : IMultiValueConverter
when you need more than one object in format string, for instance, Completed {0} tasks out of {1}
.此外,当您需要格式字符串中的多个对象时,请制作StringFormatMultiConverter : IMultiValueConverter
,例如, Completed {0} tasks out of {1}
。
I just checked and for some reason it doesn't work with the Label, probably because it uses a ContentPresenter for the Content property internally.我刚刚检查过,由于某种原因它不适用于 Label,可能是因为它在内部使用 ContentPresenter 作为 Content 属性。 You can use a TextBlock instead and that will work.您可以改用 TextBlock,这样就可以了。 You could also put the TextBlock excerpt below in the content of a Label if you need to inherit styling, behaviour etc.如果您需要继承样式、行为等,您还可以将下面的 TextBlock 摘录放在 Label 的内容中。
<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} />
Try using a converter....尝试使用转换器....
<myconverters:MyConverter x:Key="MyConverter"/>
<Label Content="{Binding Path=MaxLevelofInvestment, Converter={StaticResource MyConverter"} />
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return String.Format("Amount is {0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
Maybe this will help...也许这会有所帮助...
You can use this below你可以在下面使用这个
<Label Text="{Binding Content, StringFormat='Page Data> {0}'}" />
"Content" is binding variable and between the single quotes type your text. “内容”是绑定变量,在单引号之间键入您的文本。 {0} where the Content data will be inserted. {0} 将插入内容数据的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.