标签内容上的 WPF StringFormat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4206612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF StringFormat on Label Content
提问by Everything Matters
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
. Nothing seems to work. This is a very common use case but doesn't seem to be supported.
我什至尝试将绑定属性的数据类型更改为int
,string
和double
。似乎没有任何效果。这是一个非常常见的用例,但似乎不受支持。
回答by Ray Burns
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
属性是 type Object
,并且Binding.StringFormat
仅在绑定到 type 的属性时使用String
。
What is happening is:
正在发生的事情是:
- The
Binding
is boxing yourMaxLevelOfInvestment
value and storing it theLabel.Content
property as a boxed decimal value. - The Label control has a template that includes a
ContentPresenter
. - Since
ContentTemplate
is not set,ContentPresenter
looks for aDataTemplate
defined for theDecimal
type. When it finds none, it uses a default template. - The default template used by the
ContentPresenter
presents strings by using the label'sContentStringFormat
property.
- 将
Binding
是拳击的MaxLevelOfInvestment
价值并将其存储的Label.Content
属性作为盒装十进制值。 - Label 控件有一个模板,其中包含一个
ContentPresenter
. - 由于
ContentTemplate
未设置,ContentPresenter
查找DataTemplate
定义的Decimal
类型。当它没有找到时,它使用默认模板。 ContentPresenter
通过使用标签的ContentStringFormat
属性呈现字符串使用的默认模板。
Two solutions are possible:
有两种解决方案:
- Use Label.ContentStringFormat instead of Binding.StringFormat, or
- Use a String property such as TextBlock.Text instead of Label.Content
- 使用 Label.ContentStringFormat 而不是 Binding.StringFormat,或
- 使用 String 属性,例如 TextBlock.Text 而不是 Label.Content
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
属性,但是在加载过程中,这些自动模板绑定到 的ContentTemplate
和ContentStringFormat
属性Label
,因此看起来好像ContentPresenter
实际上是在使用Label
的属性.
回答by Michael Agroskin
Make a universal StringFormatConverter : IValueConverter
. Pass your format string as ConverterParameter
.
制作一个通用的StringFormatConverter : IValueConverter
. 将您的格式字符串作为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}
.
回答by Guy
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. You can use a TextBlock instead and that will work. You could also put the TextBlock excerpt below in the content of a Label if you need to inherit styling, behaviour etc.
我刚刚检查过,由于某种原因它不适用于 Label,可能是因为它在内部使用 ContentPresenter 作为 Content 属性。您可以改用 TextBlock,这样就可以了。如果您需要继承样式、行为等,您还可以将下面的 TextBlock 摘录放在 Label 的内容中。
<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} />
回答by Gabe
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;
}
}