wpf 将度数符号添加到标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23265104/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:37:15  来源:igfitidea点击:

Add degree symbol to label

c#wpfxaml

提问by TRS

I want to add degree symbol to this label in xaml.Please tell me how to do it?

我想在 xaml 中为这个标签添加度数符号。请告诉我该怎么做?

<Label Content="{Binding CelsiusTemperature}"  />

采纳答案by Anatoliy Nikolaev

You can use the ContentStringFormatfor Label:

您可以使用ContentStringFormatfor Label

Gets or sets a composite string that specifies how to format the Contentproperty if it is displayed as a string.

获取或设置一个复合字符串,该字符串指定在Content属性显示为字符串时如何设置它的格式。

Example:

例子:

<Label Content="{Binding Path=CelsiusTemperature}" 
       ContentStringFormat="{}{0}°" />

回答by Clemens

Use a TextBlockinstead of a Labeland set the StringFormatproperty of the Binding:

使用 aTextBlock代替 aLabel并设置StringFormatBinding的属性:

<TextBlock Text="{Binding CelsiusTemperature, StringFormat={}{0}°}"/>


If for whatever reason you really need a Label, you may set its ContentStringFormatproperty instead of the Binding's StringFormat, or just use a TextBlock as the Label's Content:

如果出于某种原因你真的需要一个标签,你可以设置它的ContentStringFormat属性而不是 Binding's StringFormat,或者只使用 TextBlock 作为 Label's Content

<Label>
    <TextBlock Text="{Binding CelsiusTemperature, StringFormat={}{0}°}"/>
</Label>