标签内容上的 WPF StringFormat
我想将字符串绑定格式设置为 Amount is X
,其中 X
是绑定到标签的属性。
我见过很多例子,但以下不起作用:
<Label Content="{Binding Path=MaxLevelofInvestment,
StringFormat='Amount is {0}'}" />
我也尝试过这些组合:
StringFormat=Amount is {0}
StringFormat='Amount is {}{0}'
StringFormat='Amount is \{0\}'
我什至尝试将绑定属性的数据类型更改为 int
、string
和 <代码>双。似乎什么都不起作用。这是一个非常常见的用例,但似乎不受支持。
I want to format my string binding as Amount is X
where X
is a property bound to a label.
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
. Nothing seems to work. This is a very common use case but doesn't seem to be supported.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不起作用的原因是
Label.Content
属性的类型为Object
,并且Binding.StringFormat
仅在绑定到时使用String
类型的属性。发生的情况是:
Binding
正在对您的MaxLevelOfInvestment
值进行装箱,并将其作为装箱的十进制值存储在Label.Content
属性中。ContentPresenter
的模板。ContentTemplate
,ContentPresenter
会查找为Decimal
类型定义的DataTemplate
。当它找不到时,它会使用默认模板。ContentPresenter
使用的默认模板通过使用标签的ContentStringFormat
属性来呈现字符串。有两种可能的解决方案:
以下是如何使用 Label.ContentStringFormat:
以下是如何使用 TextBlock:
注意:对于简单性我在上面的解释中省略了一个细节:
ContentPresenter
实际上使用它自己的Template
和StringFormat
属性,但在加载过程中这些属性会自动模板化 -绑定到Label
的ContentTemplate
和ContentStringFormat
属性,因此看起来ContentPresenter
实际上正在使用标签
的属性。The reason this doesn't work is that the
Label.Content
property is of typeObject
, andBinding.StringFormat
is only used when binding to a property of typeString
.What is happening is:
Binding
is boxing yourMaxLevelOfInvestment
value and storing it theLabel.Content
property as a boxed decimal value.ContentPresenter
.ContentTemplate
is not set,ContentPresenter
looks for aDataTemplate
defined for theDecimal
type. When it finds none, it uses a default template.ContentPresenter
presents strings by using the label'sContentStringFormat
property.Two solutions are possible:
Here is how to use Label.ContentStringFormat:
Here is how to use a TextBlock:
Note: For simplicity I omitted one detail in the above explanation: The
ContentPresenter
actually uses its ownTemplate
andStringFormat
properties, but during loading these are automatically template-bound to theContentTemplate
andContentStringFormat
properties of theLabel
, so it seems as if theContentPresenter
is actually using theLabel
's properties.制作一个通用的
StringFormatConverter : IValueConverter
。将格式字符串作为ConverterParameter
传递。另外,当您在格式字符串中需要多个对象时,请使用
StringFormatMultiConverter : IMultiValueConverter
,例如,Completed {0}tasks out of {1}
。Make a universal
StringFormatConverter : IValueConverter
. Pass your format string asConverterParameter
.Also, make
StringFormatMultiConverter : IMultiValueConverter
when you need more than one object in format string, for instance,Completed {0} tasks out of {1}
.我刚刚检查过,由于某种原因它不能与
Label
一起使用,可能是因为它在内部使用ContentPresenter
作为Content
属性。您可以使用TextBlock
来代替,这样就可以了。如果您需要继承样式、行为等,您还可以将下面的TextBlock
摘录放在Label
的内容中。I just checked and for some reason it doesn't work with the
Label
, probably because it uses aContentPresenter
for theContent
property internally. You can use aTextBlock
instead and that will work. You could also put theTextBlock
excerpt below in the content of aLabel
if you need to inherit styling, behaviour etc.尝试使用转换器....
Try using a converter....